我試圖學習一些關於C++的知識,並遇到了一些麻煩。從C++中讀取文件
我有以下代碼:
char board[5][5];
ifstream myReadFile;
myReadFile.open("c:/test.txt");
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
while (myReadFile.good())
{
board[i][j] = myReadFile.get();
cout << board[i][j];
}
}
}
myReadFile.close();
for(int x=0; x<5; x++)
{
for(int y=0; y<5; y++)
{
cout << board[x][y];
}
cout << endl;
}
現在,我期待第一個循環來讀取TXT文件中的字符和工作的,所以是的! 但第二個循環,我期待打印相同的字符回cout。然而,這是不是這樣的,所以我添加了一行
cout << board[i][j]
到第一循環,看看他們在那裏正確加載。 這是結果我得到
results http://tbmilena.dk/test.png
有人可以解釋爲什麼第二次循環不打印一樣的第一。
你能發佈text.txt文件內容嗎? – logoff 2012-03-26 15:24:52