[問題] Streams and File I/O

看板C_and_CPP (C/C++)作者 (阿聖)時間13年前 (2012/07/04 13:52), 編輯推噓1(103)
留言4則, 3人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Dec C++ 4.9.9.2 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 餵入的資料(Input): 預期的正確結果(Expected Output): 我是看原文書 原文書說會跑出一個叫numstory.txt的檔案,而且裡面會有跟story.txt一樣的資料 錯誤結果(Wrong Output): 找不到numstory.txt 自己建一個numstory.txt卻沒改變 不過後來把code裡的.TXT拿掉並且把純文字檔的檔名也去掉.TXT 就可以了 程式碼(Code):(請善用置底文網頁, 記得排版) #include <fstream> #include <iostream> #include <cstdlib> using std::ifstream; using std::ofstream; using std::cout; int main() { ifstream fin; ofstream fout; fin.open("story.txt"); if(fin.fail()) { cout<<"Input file opening failed.\n"; exit(1); } fout.open("numstory.txt"); if(fout.fail()) { cout<<"Output file opening failed.\n"; exit(1); } char next; int n=1; fin.get(next); fout<<n<<" "; while(!fin.eof()) { fout<<next; if(next=='\n') { n++; fout<<n<<' '; } fin.get(next); } fin.close(); fout.close(); return 0; } 補充說明(Supplement): 原文書Absolute C++ 4版 範例12.4 麻煩指點迷津 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 58.114.105.94

07/04 14:12, , 1F
你有按按鍵嗎?
07/04 14:12, 1F
不懂 什麼意思? ※ 編輯: fkms 來自: 58.114.105.94 (07/04 14:22) ※ 編輯: fkms 來自: 58.114.105.94 (07/04 14:22) ※ 編輯: fkms 來自: 58.114.105.94 (07/04 14:49) ※ 編輯: fkms 來自: 58.114.105.94 (07/04 14:49)

07/04 14:55, , 2F
這範例是將 story.txt 複製到 numstory.txt, 目錄下要存在
07/04 14:55, 2F

07/04 14:55, , 3F
story.txt < 專案目錄 >
07/04 14:55, 3F

07/04 15:07, , 4F
把檔案名稱用絕對路徑..如c:\story.txt 比較好找xd
07/04 15:07, 4F
文章代碼(AID): #1FyzeLXP (C_and_CPP)
文章代碼(AID): #1FyzeLXP (C_and_CPP)