所以我需要從文件中讀取一個網格,網格的寬度和長度始終是相同的。問題是當我試圖在最後一行關閉它時,它只顯示大約一半的。從文件C++讀取二維字符數組
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream TestIn("test");
int main()
{
char Grid[1000][1000],s[1000];
int LungimeX,LungimeY,i,j;
TestIn.getline(s,1000);
//finding the length
LungimeX=strlen(s);
cout<<LungimeX<<endl;
//finding the width
while (!TestIn.eof()) {
TestIn.getline(s,1000);
LungimeY++;
}
cout<<LungimeY;
//reset .eof
TestIn.clear();
TestIn.seekg(0, TestIn.beg);
//get the grid into the array
for(i=1;i<=LungimeY;i++) {
for(j=1;j<=LungimeX;j++) {
TestIn.get(Grid[i][j]);
}}
for(i=1;i<=LungimeY;i++){
for(j=1;j<=LungimeX;j++){
cout<<Grid[i][j];
}}
return 0;
}
所以是的,任何想法如何解決這個問題?
你能上傳你正在嘗試閱讀的文件嗎? – Minato
http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong –
只是一個猜測:輸入文件是否包含換行符('\ n ')?可能是的,所以他們會讀入Grid,這是你不想要的。另一件事是你使用數組爲1',C++中的索引從0開始。 Grid [0] [1000] 是錯誤的。 –