| 查看: 977 | 回復(fù): 10 | |||
| 本帖產(chǎn)生 1 個 程序強帖 ,點擊這里進行查看 | |||
xiaowu787木蟲 (正式寫手)
|
[交流]
【求助】程序運行但沒達(dá)到目的? 已有4人參與
|
||
|
想寫個程序處理一個數(shù)據(jù)文件,數(shù)據(jù)每隔四行重復(fù)一次,希望把數(shù)據(jù)分成四組(1,5,9...)(2,6,10...)(3,7,11...)(4,8,12...)(行號)分別放在四個文件中,寫了如下代碼,運行后發(fā)現(xiàn)沒有實現(xiàn)預(yù)期結(jié)果 ,請幫忙看哪里出問題了,謝謝! #include #include using namespace std; int main() { void write(); write(); return 0; } // void write() { int i; char ch[100]; ifstream read("re.dat",ios::in); for(i=0;i<8;i++) { if(i%4==0) read.getline(ch,100); ofstream write1("out1.dat",ios::app); write1< if(i%4==1) read.getline(ch,100); ofstream write2("out2.dat",ios::app); write2< if(i%4==3) read.getline(ch,100); ofstream write3("out3.dat",ios::app); write3< } } [ Last edited by xiaowu787 on 2010-9-25 at 22:02 ] |
|
#include #include using namespace std; int main() { void write(); write(); return 0; } // void write() { int i; char ch[100]; ifstream read("re.dat",ios::in); for(i=0;i<8;i++) { if(i%4==0) {//這里應(yīng)該有大括號 read.getline(ch,100); ofstream write1("out1.dat",ios::app); write1< }這里也應(yīng)該有大括號,下面幾個if也一樣,要加大括號。 if(i%4==1) read.getline(ch,100); ofstream write2("out2.dat",ios::app); write2< //i%4==2貌似漏掉了? if(i%4==3) read.getline(ch,100); ofstream write3("out3.dat",ios::app); write3< } } [ Last edited by magic7004 on 2010-9-26 at 09:17 ] |

|
幫樓主寫了個,這個可以一直讀到文件尾。如果不是文本文件,就把"\r\n"改成'\n' #include #include using namespace std; int main() { void write(); write(); return 0; } // void write() { int i=0; char ch[100]; ifstream read("re.txt",ios::in); ofstream write[]={ofstream("out1.txt",ios::app), ofstream("out2.txt",ios::app), ofstream("out3.txt",ios::app), ofstream("out4.txt",ios::app)}; while(read.getline(ch,100)) { write[(i++)%4]< } } [ Last edited by magic7004 on 2010-9-26 at 09:25 ] |

|
木蟲 (正式寫手)
|
編譯的時候提示錯誤:您看看哪里有問題吧,謝謝! [u06@pc07 C]$ c++ -o data2 data2.cpp /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/ios_base.h: In copy constructor ‘std::basic_ios ’:/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/ios_base.h:779: 錯誤:‘std::ios_base::ios_base(const std::ios_base& ’ 是私有的/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/iosfwd:55: 錯誤:在此上下文中 /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/iosfwd: In copy constructor ‘std::basic_ofstream ’:/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/iosfwd:92: 附注:在這里第一次需要生成的方法 ‘std::basic_ios ’ |

木蟲 (正式寫手)
|
看那個編譯錯誤的提示,估計是ofstream那個數(shù)組的問題吧。改成ofstream*的數(shù)組試試看呢。 #include #include using namespace std; int main() { void write(); write(); return 0; } // void write() { int i=0; char ch[100]; ifstream read("re.txt",ios::in); ofstream *write[]={new ofstream("out1.txt",ios::app), new ofstream("out2.txt",ios::app), new ofstream("out3.txt",ios::app), new ofstream("out4.txt",ios::app)}; while(read.getline(ch,100)) { *(write[(i++)%4])< } for(int j=0; j<4; j++) delete write[j]; } |

木蟲 (正式寫手)

| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 初試 317 +7 | 半拉月丙 2026-03-20 | 7/350 |
|
|---|---|---|---|---|
|
[考研] 廣西大學(xué)材料導(dǎo)師推薦 +3 | 夏夏夏小正 2026-03-17 | 5/250 |
|
|
[考研] 333求調(diào)劑 +5 | 87639 2026-03-21 | 7/350 |
|
|
[考研] 求助 +5 | 夢里的無言 2026-03-21 | 6/300 |
|
|
[基金申請]
學(xué)校已經(jīng)提交到NSFC,還能修改嗎?
40+4
|
babangida 2026-03-19 | 9/450 |
|
|
[考研] 279分求調(diào)劑 一志愿211 +14 | chaojifeixia 2026-03-19 | 15/750 |
|
|
[考研] 332求調(diào)劑 +3 | 鳳凰院丁真 2026-03-20 | 3/150 |
|
|
[考研] 08工科 320總分 求調(diào)劑 +6 | 梨花珞晚風(fēng) 2026-03-17 | 6/300 |
|
|
[考研] 311求調(diào)劑 +5 | 冬十三 2026-03-18 | 5/250 |
|
|
[考研] 304求調(diào)劑 +7 | 司空. 2026-03-18 | 7/350 |
|
|
[考研] 288求調(diào)劑 +16 | 于海海海海 2026-03-19 | 16/800 |
|
|
[考研] 287求調(diào)劑 +7 | 晨昏線與星海 2026-03-19 | 8/400 |
|
|
[考研] 一志愿武理材料工程348求調(diào)劑 +3 |  ̄^ ̄゜汗 2026-03-19 | 4/200 |
|
|
[考研] 0817 化學(xué)工程 299分求調(diào)劑 有科研經(jīng)歷 有二區(qū)文章 +22 | rare12345 2026-03-18 | 22/1100 |
|
|
[考研] 319求調(diào)劑 +3 | 小力氣珂珂 2026-03-20 | 3/150 |
|
|
[考研] 085410人工智能專碩317求調(diào)劑(0854都可以) +4 | xbxudjdn 2026-03-18 | 4/200 |
|
|
[考博] 申博26年 +3 | 八6八68 2026-03-19 | 3/150 |
|
|
[考研] 328求調(diào)劑,英語六級551,有科研經(jīng)歷 +4 | 生物工程調(diào)劑 2026-03-16 | 12/600 |
|
|
[考研] 277調(diào)劑 +5 | 自由煎餅果子 2026-03-16 | 6/300 |
|
|
[考研] 有沒有道鐵/土木的想調(diào)劑南林,給自己招師弟中~ +3 | TqlXswl 2026-03-16 | 7/350 |
|