Do .. While Loop/Textfile/Operation Problem
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 rea开发者_如何转开发d 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!
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 with
<CR><LF>1
<LF>1
1
All of which result in s
being 1.
The same is repeated for 2, 3 and so on.
Forget yout seekp() and simply use
while (file.good()) {
file >> s; // skip line
if (!file.good()) break;
file >> s;
cout << s;
stats[s]++;
}
精彩评论