[問題] c++讀檔問題--讀的檔案太大?

看板C_and_CPP (C/C++)作者 (fatdog)時間15年前 (2010/10/22 21:56), 編輯推噓4(4020)
留言24則, 4人參與, 最新討論串1/1
( *[1m *[m 為色碼,可以按 Ctrl+V 預覽會顯示的顏色 ) ( 未必需要依照此格式,文章條理清楚即可 ) 遇到的問題: (題意請描述清楚) 這是我的程式碼,讀檔用的,共讀了6筆.txt檔,但是讀出來時,他只讀了後面四筆,前面的都沒有出現, 一筆.txt檔,共有70個值和說明文字是直列的 以下的程式是在vc++6.0執行的 #include "stdafx.h" #include<iostream.h> #include<fstream.h> void main() { ifstream Table1; int record_number1=0; char record1[1000]; Table1.open("D:\\user1\\save0.txt"); if(!Table1.is_open()) { cout<<"file is failed"<<endl; } else { while(!Table1.eof()) { Table1.getline(record1,sizeof(record1)); record_number1++; cout<<record1<<endl; } } ifstream Table2; int record_number2=0; char record2[1000]; Table2.open("D:\\user1\\save1.txt"); if(!Table2.is_open()) { cout<<"file is failed"<<endl; } else { while(!Table2.eof()) { Table2.getline(record2,sizeof(record2)); record_number2++; cout<<record2<<endl; } } ifstream Table3; int record_number3=0; char record3[1000]; Table3.open("D:\\user1\\save2.txt"); if(!Table3.is_open()) { cout<<"file is failed"<<endl; } else { while(!Table3.eof()) { Table3.getline(record3,sizeof(record3)); record_number3++; cout<<record3<<endl; } } ifstream Table4; int record_number4=0; char record4[1000]; Table4.open("D:\\user1\\save3.txt"); if(!Table4.is_open()) { cout<<"file is failed"<<endl; } else { while(!Table4.eof()) { Table4.getline(record4,sizeof(record4)); record_number4++; cout<<record4<<endl; } } ifstream Table5; int record_number5=0; char record5[1000]; Table5.open("D:\\user1\\save4.txt"); if(!Table5.is_open()) { cout<<"file is failed"<<endl; } else { while(!Table5.eof()) { Table5.getline(record5,sizeof(record5)); record_number5++; cout<<record5<<endl; } } ifstream Table6; int record_number6=0; char record6[1000]; Table6.open("D:\\user1\\save5.txt"); if(!Table6.is_open()) { cout<<"file is failed"<<endl; } else { while(!Table6.eof()) { Table6.getline(record6,sizeof(record6)); record_number6++; cout<<record6<<endl; } } Table1.close(); Table2.close(); Table3.close(); Table4.close(); Table5.close(); Table6.close(); } 希望得到的正確結果: 六筆資料全讀出來 程式跑出來的錯誤結果: 開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux) 有問題的code: (請善用置底文標色功能) 補充說明: -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.160.19.211

10/22 22:21, , 1F
因為你配的空間只夠存一行字, 而一行字存4筆資料, 所
10/22 22:21, 1F

10/22 22:22, , 2F
以最後只存4筆, 還有你不覺得重複的部份很多嗎? 寫程
10/22 22:22, 2F

10/22 22:22, , 3F
是不是只有複製貼上耶...
10/22 22:22, 3F

10/22 22:31, , 4F
謝謝你的解釋,原諒我只是新手,第一次碰此程式語言,所
10/22 22:31, 4F

10/22 22:32, , 5F
以很多地方都會出現令人啼笑皆非的問題,請多包含
10/22 22:32, 5F

10/22 22:35, , 6F
那請問應該怎麼改善比較好呢?
10/22 22:35, 6F

10/22 22:38, , 7F
重複的地方寫成函式, 這樣你就不用傷腦筋在命名上, 導
10/22 22:38, 7F

10/22 22:38, , 8F
至會有 a1, a2, a3, 這種名字出現, 這就好像人名, 總
10/22 22:38, 8F

10/22 22:39, , 9F
不能三兄弟叫 小明1 小明2 小明3, 這是很糟的寫法
10/22 22:39, 9F

10/22 22:40, , 10F
如果是用陣列來存, 還可以表現出他們的關聯性, 另外
10/22 22:40, 10F

10/22 22:40, , 11F
都已經寫C++了, 讀字串我不建議用C的字元陣列來做, 而
10/22 22:40, 11F

10/22 22:41, , 12F
且在不知道檔案大小的情況下, 動態記憶體配置容易出錯
10/22 22:41, 12F

10/22 22:42, , 13F
用二維陣列硬上又會浪費空間, 最好還是使用STL容器來
10/22 22:42, 13F

10/22 22:42, , 14F
裝 string 物件
10/22 22:42, 14F

10/22 22:49, , 15F
謝謝我會再加油的
10/22 22:49, 15F

10/22 22:55, , 16F
你是不是跳著看,或者直接上網查怎樣讀檔案內容?
10/22 22:55, 16F

10/22 22:56, , 17F
去買本新一點的書至少不會教你用 void main,把基礎打好吧
10/22 22:56, 17F

10/22 23:03, , 18F
這是我寫的讀檔程式 http://codepad.org/7otClcF4
10/22 23:03, 18F

10/22 23:04, , 19F
使用它之前會先讀熟 exception 的繼承結構, iostream
10/22 23:04, 19F

10/22 23:04, , 20F
家族的繼承結構, 還有 STL 容器跟演算法的用法, 以及
10/22 23:04, 20F

10/22 23:05, , 21F
string類別的函式庫, 我覺得這些算是 C++ 的基本, 所
10/22 23:05, 21F

10/22 23:06, , 22F
以就直接這樣寫了, 讀熟一定對你有所幫助
10/22 23:06, 22F

10/22 23:10, , 23F
是...謝謝您
10/22 23:10, 23F

10/25 07:31, , 24F
readFileAndSaveInVector 好帥的名字!
10/25 07:31, 24F
文章代碼(AID): #1CmPWbBM (C_and_CPP)
文章代碼(AID): #1CmPWbBM (C_and_CPP)