【发布时间】:2018-12-10 02:53:01
【问题描述】:
我正在尝试使用我已经使用过并且被证明是成功的方法将元素添加到我的文件中,但是现在当我这样做时,我得到了我想要的数字以及一堆其他不是的数字在我的文件中,没有任何意义
const int MAX_SIZE = 21;
int readSquare(int square[MAX_SIZE][MAX_SIZE], string inputFileName){ //reads file into an array
int value;
ifstream inFile;
inFile.open(inputFileName);
if (inFile) //if the input file to be read open successfully then goes on
{
int temp;
inFile >> temp;
if (temp>21) {
temp=21;
}
for (int i = 0; i < MAX_SIZE; i++)
{
for(int j = 0; j < MAX_SIZE; j++)
{
inFile >> square[i][j];
}
}
} else {
inFile.close();
return 0; //returns 0 if couldnt open file
}
inFile.close();
cout << "Magic square" << endl;
for(int i=0;i<MAX_SIZE;i++)
{
for(int j=0;j<MAX_SIZE;j++)
{
cout << square[i][j] << " ";
}
cout<<endl;
}
return 1;
}
这是我在代码中使用的文件
3
4 9 2
3 5 7
8 1 6
这是我得到的结果(持续了一段时间,但我只拿了顶部)
4 9 2 3 5 7 8 1 6 16840768 6619136 6643024 23198772 0 1942212500 127 917504 6643024 786434 6643032 0
65536 30 0 31 0 13930549 30 593 6619744 6619744 -2 127 46 6420808 1997546816 -1759127226 -2 6420704 1997359545 4096 4104
0 6420680 6634144 6619136 6421232 4104 6619744 0 3 0 4096 6420732 1997535944 6420804 655612 655360 2 9 0 2 6420976
0 1997378284 6420976 663276 1952 229640288 663200 655360 0 1997377793 6421060 661336 9 16777596 0 13080 236 661336 2 16777596 -530786634
【问题讨论】:
-
你在用
temp做什么? -
@bvj 第一行告诉我文件中有多少行,我不希望将其添加到数组中,我将最大行设置为 21。此外,我已经尝试使用 melpomene 进行调试那些标准。我想我错过了什么
-
@Unknownzdx bvj 没有询问第一行,而是变量
temp。您的代码实际上对temp做了什么? -
@melpomene 它在文本中抓取文件的第一行,如果值大于 21,则将其设置为 21。除此之外它不做任何事情
标签: c++ multidimensional-array