来自.text文件的C ++ 2d数组

时间:2013-04-10 16:27:19

标签: c++ arrays file file-io multidimensional-array

我的代码打印出一个.txt文件的wacked out版本,该文件显示一个20x20的字符表和空格。如何才能使数组正确显示,就像在.txt中一样。我不能使用向量或全局变量。没有那些就可以完成。文本中的前两行是20和20,以获取数组的尺寸。

ifstream inputFile;
int boardSizeRow;
int boardSizeCol;
inputFile.open("fileboard1.txt");
inputFile >> boardSizeRow;
inputFile >> boardSizeCol;
inputFile.get();

char gameBoard[20][20];
for (int row = 0; row < boardSizeRow; row++)
{
    for (int col = 0; col < boardSizeCol; col++)
    {
        gameBoard[row][col] = inputFile.get();
    }
}

for (int row = 0; row < boardSizeRow; row++) //////////////TO TEST PRINT
{
    for (int col = 0; col < boardSizeCol; col++)
    {
        cout << gameBoard[row][col];
    }
    inputFile.get();
    cout << endl;
}
return 0;


20

20

WWWWWWWWWWWWWWWWWWWW
  W GO  W          W
W WW      w    S   W      
W   W   GW  w      W  
WPW  WW          G W    
 WK       W        W     
W W W  W    w   w  W  
W WK W             W    
W   SW  U    w  w  W
                   W
    w          G   W
  G         w    w W 
D   wwwww          W
             w  D  W
w w   W w   w      W
    ww  w     w w  W
  G        w       W
    ww  w S    w   W
   WWW      G      W
WWWWWWWWWWWWWWWWWWWW

3 个答案:

答案 0 :(得分:3)

这是答案:) 好的几个编辑: 输入:

   3
   3
   2 2 3 
   2 2 3 
   2 2 3 

代码:

#include <fstream>
#include <iostream>

int main(){
using namespace std;
ifstream inputFile;
int boardSizeRow;
int boardSizeCol;
inputFile.open("fileboard1.txt");
inputFile >> boardSizeRow;
inputFile >> boardSizeCol;

char *gameBoard= new char[boardSizeRow*boardSizeCol];
for (int row = 0; row < boardSizeRow; row++)
{
    for (int col = 0; col < boardSizeCol; col++)
    {   
        inputFile >> *(gameBoard + boardSizeCol * row + col);
    }   
}

for (int row = 0; row < boardSizeRow; row++) //////////////TO TEST PRINT
{
    for (int col = 0; col < boardSizeCol; col++)
    {   
        cout << *(gameBoard + boardSizeCol * row + col) << " ";
    }   
    cout << endl;
}
delete []gameBoard
return 0;

}

答案 1 :(得分:2)

来自How do I declare a 2d array in C++ using new?

只是做:

inputFile >> boardSizeRow;
inputFile >> boardSizeCol;

char **gameBoardRow = new char*[boardSizeCol];
for(int i = 0; i < sizeY; ++i) {
    ary[i] = new char[boardSizeRow];
}

for (int row = 0; row < boardSizeRow; row++)
{
    for (int col = 0; col < boardSizeCol; col++)
    {   
        inputFile >> gameBoard[row][col];
     }   
}

for (int row = 0; row < boardSizeRow; row++) //////////////TO TEST PRINT
{
    for (int col = 0; col < boardSizeCol; col++)
    {   
        cout << gameBoard[row][col] << " ";
    }   
    cout << endl; 
}

for(int i = 0; i < boardSizeCol; ++i) {
    delete [] gameBoard[boardSizeRow];
}
delete [] gameBoard;

答案 2 :(得分:0)

该文件包含行之间的换行符,您无法处理。因此,在阅读完第一行后,您将读取换行符中的一个(或两个Windows平台)字符作为第二行中的第一个字符。

我建议您使用std::vector std::string。每行一个字符串。

类似的东西:

inputFile >> boardSizeRows;
inputFile >> boardSizeCols;

// Skip newline after the number of columns
inputFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

// And the empty line after that
inputFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

// Declare our game board
std::vector<std::string> gameBoard;

// Read the board
std::string line;
for (int row = 0; row < boardSizeRows && std::getline(inputFile, line); row++)
    gameBoard.push_back(line);

// And finally print the board
for (const std::string& l : gameBoard)
    std::cout << l << '\n';

读取文件时for循环中的条件是首先检查我们读取的内容不超过文件中的请求,并且读取操作也成功。


如果你的编译器无法处理C++11 range-base for-loops(用于打印板),你必须使用迭代器“老式”方式:

std::vector<std::string>::const_iterator li;
for (li = gameBoard.begin(); li != gameBoard.end(); li++)
    std::cout << *li << '\n';