[問題] 讀檔問題

看板C_and_CPP作者 (工人)時間15年前 (2009/12/27 13:35), 編輯推噓1(102)
留言3則, 1人參與, 最新討論串2/9 (看更多)
( *[1m *[m 為色碼,可以按 Ctrl+V 預覽會顯示的顏色 ) ( 未必需要依照此格式,文章條理清楚即可 ) 遇到的問題: (題意請描述清楚) 1 5 紅處是多一空格 8 -9 為何程式就會亂跑 如果把空格去掉(就是在5後面不要多按一space) 就正確 願聞其詳 謝 有問題的code: (請善用置底文標色功能) #include <iostream> #include <iomanip> using namespace std; #include <string> #include <fstream> #include <cstdlib> int main() { string filename; int deter_row, deter_column, numA[10][10]; char check; int sum=0,row=0,column=0,k; ifstream read; cin>>filename; read.open(filename.c_str()); if (read.fail()) // check for a successful open { cout << "\nThe file was not successfully opened" << "\n Please check that the file currently exists." <<endl; system("pause"); exit(0); } cout << "\nThe file has been successfully opened for reading"<< endl; do { read>>numA[row][column]; column=column+1; if((check=read.get())=='\n') { row=row+1; column=0; } }while(read.good()); for(int ii=0;ii<=row;ii++) { for(int jj=0;jj<column;jj++) { cout<<setw(3)<<numA[ii][jj]; } cout<<endl; } deter_row=row+1; deter_column=column; system("pause"); return 0; } 編譯過的 補充說明: -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.7.59

12/28 09:51, , 1F
正是因為你在行尾給了空白...
12/28 09:51, 1F

12/28 09:52, , 2F
仔細想想沒有空白時 if((check=read.get())=='\n') 的機制
12/28 09:52, 2F

12/28 09:52, , 3F
去想想為什麼在沒有空白時這樣可以計算行數
12/28 09:52, 3F
文章代碼(AID): #1BDsAqQ_ (C_and_CPP)
討論串 (同標題文章)
完整討論串 (本文為第 2 之 9 篇):
問題
1
13
問題
0
1
問題
9
34
問題
0
13
問題
3
18
問題
3
13
問題
0
10
問題
1
3
問題
0
1
文章代碼(AID): #1BDsAqQ_ (C_and_CPP)