Do .. While 循环/文本文件/操作问题
您好,我对以下代码有疑问:
int skp = 1;
do{
file.seekp(skp);
file>>s;
cout<<s;
stats[s]++;
skp++;
skp++;
}while(skp <= 10);
文本文件具有以下内容: 0
1
2
3
0
1
0
1
0
我想要这个编程做的是从读取它执行的第二个数字开始,然后跳过一个读取下一个,跳过一个读取下一个等等。它所做的是读取第二个数字这很好,然后再读2遍,然后读下一个数字3遍,再读下一个3遍。所以我从上面的文本文件收到的输出是 1112223330。
请问谁能帮帮我吗? 谢谢你!
Hi I have a problem with the following code:
int skp = 1;
do{
file.seekp(skp);
file>>s;
cout<<s;
stats[s]++;
skp++;
skp++;
}while(skp <= 10);
The Textfile has the following:
0
1
2
3
0
1
0
1
0
What I want this programming to do is start from reading the second number which it does, then skip one read next, skip one read the next etc. etc. what it's doing is read the second number which is good, then reads it again for 2 times, then read the next number for 3 times and the next for 3 times. So the output i receive from the above textfile is
1112223330.
Can any one help me please!
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您的行是由换行符(实际上是 CR 和 LF)分隔的。另外,
文件>> s
将跳过前导空格,因此最终会得到所有结果,
s
为 1。对于 2、3 等重复相同的操作。
忘记seekp()并简单地使用
That's because your lines are separated by line feeds (actually CR and LF). Also,
file >> s
will skip leading white space, so you end up withAll of which result in
s
being 1.The same is repeated for 2, 3 and so on.
Forget yout seekp() and simply use