| 查看: 983 | 回復(fù): 10 | |||
| 本帖產(chǎn)生 1 個(gè) 程序強(qiáng)帖 ,點(diǎn)擊這里進(jìn)行查看 | |||
xiaowu787木蟲 (正式寫手)
|
[交流]
【求助】程序運(yùn)行但沒達(dá)到目的? 已有4人參與
|
||
|
想寫個(gè)程序處理一個(gè)數(shù)據(jù)文件,數(shù)據(jù)每隔四行重復(fù)一次,希望把數(shù)據(jù)分成四組(1,5,9...)(2,6,10...)(3,7,11...)(4,8,12...)(行號(hào))分別放在四個(gè)文件中,寫了如下代碼,運(yùn)行后發(fā)現(xiàn)沒有實(shí)現(xiàn)預(yù)期結(jié)果 ,請(qǐng)幫忙看哪里出問題了,謝謝! #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 ] |
金蟲 (職業(yè)作家)
|
#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)該有大括號(hào) read.getline(ch,100); ofstream write1("out1.dat",ios::app); write1< }這里也應(yīng)該有大括號(hào),下面幾個(gè)if也一樣,要加大括號(hào)。 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 ] |

金蟲 (職業(yè)作家)
|
幫樓主寫了個(gè),這個(gè)可以一直讀到文件尾。如果不是文本文件,就把"\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 ] |

|
木蟲 (正式寫手)
|
編譯的時(shí)候提示錯(cuò)誤:您看看哪里有問題吧,謝謝! [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: 錯(cuò)誤:‘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: 錯(cuò)誤:在此上下文中 /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 ’ |
金蟲 (職業(yè)作家)

木蟲 (正式寫手)
金蟲 (職業(yè)作家)
|
看那個(gè)編譯錯(cuò)誤的提示,估計(jì)是ofstream那個(gè)數(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]; } |

木蟲 (正式寫手)
金蟲 (職業(yè)作家)

| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研]
|
pk3725069 2026-03-19 | 14/700 |
|
|---|---|---|---|---|
|
[考研] 336求調(diào)劑 +3 | 收到VS 2026-03-20 | 3/150 |
|
|
[考研] 276求調(diào)劑 +3 | YNRYG 2026-03-21 | 4/200 |
|
|
[考研] 323求調(diào)劑 +6 | 洼小桶 2026-03-18 | 6/300 |
|
|
[考研] 求調(diào)劑 +6 | 十三加油 2026-03-21 | 6/300 |
|
|
[考研] 環(huán)境學(xué)碩288求調(diào)劑 +6 | 皮皮皮123456 2026-03-22 | 6/300 |
|
|
[考研] 考研調(diào)劑 +4 | 來好運(yùn)來來來 2026-03-21 | 4/200 |
|
|
[考研] 285求調(diào)劑 +6 | ytter 2026-03-22 | 6/300 |
|
|
[考研] 資源與環(huán)境 調(diào)劑申請(qǐng)(333分) +5 | holy J 2026-03-21 | 5/250 |
|
|
[考研] 材料求調(diào)劑 +5 | @taotao 2026-03-21 | 5/250 |
|
|
[考研] 考研調(diào)劑 +3 | 呼呼?~+123456 2026-03-21 | 3/150 |
|
|
[考研] 278求調(diào)劑 +9 | 煙火先于春 2026-03-17 | 9/450 |
|
|
[考研] 0805材料320求調(diào)劑 +3 | 深海物語 2026-03-20 | 3/150 |
|
|
[考研] 材料學(xué)學(xué)碩080502 337求調(diào)劑-一志愿華中科技大學(xué) +4 | 順順順mr 2026-03-18 | 5/250 |
|
|
[考研] 一志愿蘇州大學(xué)材料求調(diào)劑,總分315(英一) +5 | sbdksD 2026-03-19 | 5/250 |
|
|
[考研] 一志愿南理工085701環(huán)境302求調(diào)劑院校 +3 | 葵梓衛(wèi)隊(duì) 2026-03-20 | 3/150 |
|
|
[考研] 材料考研調(diào)劑 +3 | xwt。 2026-03-19 | 3/150 |
|
|
[考研] 本科鄭州大學(xué)物理學(xué)院,一志愿華科070200學(xué)碩,346求調(diào)劑 +4 | 我不是一根蔥 2026-03-18 | 4/200 |
|
|
[考研] 考研求調(diào)劑 +3 | 橘頌. 2026-03-17 | 4/200 |
|
|
[考研] 有沒有道鐵/土木的想調(diào)劑南林,給自己招師弟中~ +3 | TqlXswl 2026-03-16 | 7/350 |
|