Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;来自.txt文件的二维数组_C++_Arrays_File_File Io_Multidimensional Array - Fatal编程技术网

C++ C++;来自.txt文件的二维数组

C++ C++;来自.txt文件的二维数组,c++,arrays,file,file-io,multidimensional-array,C++,Arrays,File,File Io,Multidimensional Array,我的代码打印出一个.txt文件的压缩版本,该文件显示一个20x20的字符和空格表。如何使数组像在.txt中那样正确显示。我不能使用向量或全局变量。没有这些就可以做到。文本中的前两行是20和20,用于获取数组的维度 ifstream inputFile; int boardSizeRow; int boardSizeCol; inputFile.open("fileboard1.txt"); inputFile >> boardSizeRow; inputFile >> b

我的代码打印出一个.txt文件的压缩版本,该文件显示一个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
ifstream输入文件;
int boardSizeRow;
国际董事会;
打开(“fileboard1.txt”);
输入文件>>boardSizeRow;
输入文件>>boardSizeCol;
get();
char gameBoard[20][20];
对于(int row=0;row
3
   3
   2 2 3 
   2 2 3 
   2 2 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;
#包括
#包括
int main(){
使用名称空间std;
ifstream输入文件;
int boardSizeRow;
国际董事会;
打开(“fileboard1.txt”);
输入文件>>boardSizeRow;
输入文件>>boardSizeCol;
char*gameBoard=new char[boardSizeRow*boardSizeCol];
对于(int row=0;row>*(游戏板+boardSizeCol*行+列);
}   
}
用于(int row=0;row
3
   3
   2 2 3 
   2 2 3 
   2 2 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;
#包括
#包括
int main(){
使用名称空间std;
ifstream输入文件;
int boardSizeRow;
国际董事会;
打开(“fileboard1.txt”);
输入文件>>boardSizeRow;
输入文件>>boardSizeCol;
char*gameBoard=new char[boardSizeRow*boardSizeCol];
对于(int row=0;row>*(游戏板+boardSizeCol*行+列);
}   
}
用于(int row=0;rowcout文件包含行与行之间的换行符,您不需要处理。因此,在读取第一行后,您可以将换行符的一个(或两个在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';
inputFile>>boardSizeRows;
输入文件>>boardSizeCols;
//在列数之后跳过换行符
忽略(std::numeric_limits::max(),'\n');
//然后是空行
忽略(std::numeric_limits::max(),'\n');
//申报我们的游戏板
矢量游戏板;
//读黑板
std::字符串行;
对于(int row=0;rowstd::cout文件包含行与行之间的换行符,您不需要处理它。因此,在读取第一行后,您可以将换行符的一个(或两个在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';
inputFile>>boardSizeRows;
输入文件>>boardSizeCols;
//在列数之后跳过换行符
忽略(std::numeric_limits::max(),'\n');
//然后是空行
忽略(std::numeric_limits::max(),'\n');
//申报我们的游戏板
矢量游戏板;
//读黑板
std::字符串行;
对于(int row=0;row
只要做:

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;
inputFile>>boardSizeRow;
输入文件>>boardSizeCol;
字符**gameBoardRow=新字符*[boardSizeCol];
对于(int i=0;i>游戏板[行][col];
}   
}
用于(int row=0;row
只要做:

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;
inputFile>>boardSizeRow;
输入文件>>boardSizeCol;
字符**gameBoardRow=新字符*[boardSizeCol];
对于(int i=0;i>游戏板[行][col];
}   
}
用于(int row=0;row您能不能显示您正在阅读的文件?它是否包含字符之间的空格?它是否包含换行符?另外,为什么在第二个循环中使用
inputFile.get();
(就在
cout@JoachimPileborg的上方,文件被添加了
cout@camelcc,我运行了它,这样我就可以看到游戏板是如何读取的印加。请显示您正在读取的文件?它是否包含字符之间的空格?它是否包含换行符?另外,为什么在第二个循环中使用
inputFile.get();
)(在上面的代码> CuthJoaPixIbrBG文件中添加了代码> Cuth@ CAMELCCC,我运行了它,这样我就可以看到游戏板是如何被读取的,它不是,不是完全。C++没有可变长度的数组,所以声明<代码>游戏板无效。而且,你没有得到新的行。Char游戏板[ BoasisiZelo][boardSizeRow];我确信您必须在那里使用常量值,而不是变量。另一种方法是:
char