作者lyle12345 (kl)
看板C_and_CPP
標題[問題] 二維資料讀取,發生錯誤
時間Fri May 15 00:18:19 2009
a.dat 裡面資料是: 1 2 3 4
6 7 8 9
int main()
{
int mat[3][1];
ifstream data_file("a.dat",ios::in);
ofstream out_file("b.dat",ios::out);
int i,j;
for (int i=0;i <= 3;i++){
for (int j=0;j <= 1;j++){
data_file >> mat[i][j];
cout << mat[i][j];
out_file << mat[i][j]; }}
cout <<" mat[0][0] = "<<mat[0][0];
cout <<" mat[1][0] = "<<mat[1][0];
cout <<" mat[2][0] = "<<mat[2][0];
cout <<" mat[3][0] = "<<mat[3][0]<<"\n";
在for裡面cout <<mat[i][j] 顯示正確的12346789
在cout <<mat[0][0] 之後卻顯示 1 3 6 8 ...
我試著放在for迴圈讀也是不正確 不知道為什麼~~~...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.167.9.139
※ 編輯: lyle12345 來自: 118.167.9.139 (05/15 00:23)
1F:推 VictorTom:你的mat[][]大小開不夠吧?? 要4x2的你只開了3x1.... 05/15 01:18
2F:→ lyle12345:所以是改int mat[4][2] 05/15 01:21
3F:→ lyle12345:是嗎@@? 05/15 01:21
4F:推 VictorTom:是; 話說自己試試會更有fu喔, 另外這種問題可以學的搭 05/15 01:24
5F:→ VictorTom:debugger觀察mat[][]每個element的值在每一個step的變 05/15 01:24
6F:→ VictorTom:化也可以幫助你自己找到錯誤的:) 05/15 01:25