| 查看: 915 | 回复: 10 | |||
| 本帖产生 1 个 程序强帖 ,点击这里进行查看 | |||
| 当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖 | |||
xiaowu787木虫 (正式写手)
|
[交流]
【求助】程序运行但没达到目的? 已有4人参与
|
||
|
想写个程序处理一个数据文件,数据每隔四行重复一次,希望把数据分成四组(1,5,9...)(2,6,10...)(3,7,11...)(4,8,12...)(行号)分别放在四个文件中,写了如下代码,运行后发现没有实现预期结果 ,请帮忙看哪里出问题了,谢谢! #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 ] |
» 猜你喜欢
不自信的我
已经有11人回复
北核录用
已经有3人回复
要不要辞职读博?
已经有6人回复
实验室接单子
已经有3人回复
磺酰氟产物,毕不了业了!
已经有8人回复
求助:我三月中下旬出站,青基依托单位怎么办?
已经有10人回复
26申博(荧光探针方向,有机合成)
已经有4人回复
论文终于录用啦!满足毕业条件了
已经有26人回复
2026年机械制造与材料应用国际会议 (ICMMMA 2026)
已经有4人回复
Cas 72-43-5需要30g,定制合成,能接单的留言
已经有8人回复
» 本主题相关价值贴推荐,对您同样有帮助:
【求助】wien2k08b安装和试运行中遇到的问题
已经有19人回复
【求助】突然出现程序消失但依然占据CPU的情况?
已经有3人回复
【整理】《提问的智慧》——献给那些不会提问的人
已经有78人回复
【求助】fortran程序未出错,但运行不出结果,中途中断!
已经有6人回复
【求助】修改MATLAB支持向量机核函数程序遇到的疑问?求助
已经有5人回复
★ ★ ★ ★ ★ ★
小木虫(金币+0.5):给个红包,谢谢回帖交流
xiaowu787(金币+3):您对编程相当的专注! 2010-09-27 14:31:15
nono2009(金币+5, 程序强帖+1):耐心指导 2010-09-30 08:40:33
小木虫(金币+0.5):给个红包,谢谢回帖交流
xiaowu787(金币+3):您对编程相当的专注! 2010-09-27 14:31:15
nono2009(金币+5, 程序强帖+1):耐心指导 2010-09-30 08:40:33
|
看那个编译错误的提示,估计是ofstream那个数组的问题吧。改成ofstream*的数组试试看呢。 #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]; } |

8楼2010-09-26 16:48:28
★
小木虫(金币+0.5):给个红包,谢谢回帖交流
xiaowu787(金币+3):谢谢你详细的分析 2010-09-26 10:08:35
小木虫(金币+0.5):给个红包,谢谢回帖交流
xiaowu787(金币+3):谢谢你详细的分析 2010-09-26 10:08:35
|
#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也一样,要加大括号。 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 ] |

2楼2010-09-26 09:15:57
★
小木虫(金币+0.5):给个红包,谢谢回帖交流
xiaowu787(金币+2):谢谢您的热心帮助 2010-09-26 10:09:02
小木虫(金币+0.5):给个红包,谢谢回帖交流
xiaowu787(金币+2):谢谢您的热心帮助 2010-09-26 10:09:02
|
帮楼主写了个,这个可以一直读到文件尾。如果不是文本文件,就把"\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 ] |

3楼2010-09-26 09:20:00
xiaowu787
木虫 (正式写手)
- 应助: 1 (幼儿园)
- 金币: 1411.9
- 散金: 95
- 红花: 1
- 帖子: 550
- 在线: 430.1小时
- 虫号: 326816
- 注册: 2007-03-18
- 性别: GG
|
编译的时候提示错误:您看看哪里有问题吧,谢谢! [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 ’ |
5楼2010-09-26 11:58:36












回复此楼
’: