File I/O Question

S

solartimba

I am having a file I/O problem that I can't seem to solve. The
following code is used to read in a file and write the same data:

CODE
while(infile)
{
infile >> num1 >> num2;
cout << num1 << " " << num2 << endl;
outfile << num1 << " " << num2 << endl;
}

INFILE
1 11
2 22
3 33
4 44
5 55
6 66
7 77

OUTFILE
1 11
2 22
3 33
4 44
5 55
6 66
7 77
77

Why is this repeating half of the final line of data?
 
R

Ron Natalie

solartimba said:
I am having a file I/O problem that I can't seem to solve. The
following code is used to read in a file and write the same data:

CODE
while(infile)

Why is this repeating half of the final line of data?

Because the stream doesn't enter an error state until AFTER
you do an input that fails.

Try this:
while(infile >> num1 >> num2) {
cout << ...
outfile << ...
}
 
U

user

solartimba said:
I am having a file I/O problem that I can't seem to solve. The
following code is used to read in a file and write the same data:

CODE
while(infile)

You should write while (infile >> num1 >> num2) {
...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Minimum Total Difficulty 0
Derived class question 9
Register Question 0
Taskcproblem calendar 4
Runtime Error with __gcd? (floating point exception) 1
fstream File i/o 1
I need help 1
Chatbot 0

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top