Computing.Net > Forums > Programming > Problem reading file in c++

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Problem reading file in c++

Reply to Message Icon

Name: rameshovyas
Date: September 13, 2003 at 19:58:00 Pacific
OS: DOS
CPU/Ram: Intel P3 /128 MB
Comment:

I have written a code to retrive data from a file in
C++,as follows

cStud S;
while(!f.eof())
{
f.read((char*)&S,sizeof(S));
coutS;
}

where cStud is any class of mine and f is an object of ifstream.

The problem i am facing is that it prints the last record twice.
But if i write the code as follows there is no problem

cStud S;
while( f.read((char*)&S,sizeof(S)))
{
coutS;
}

Where is the problem in the previous code,its urgent



Sponsored Link
Ads by Google

Response Number 1
Name: borelli35
Date: September 13, 2003 at 22:05:26 Pacific
Reply:

=================================================
This can happen if the data being read into your variables data type differs from that of the file itself. A string read with a char * that doesn't contain an EOL or null terminated string can confuse the cout function (I'm assuming that their is a space between the cout function and your S variable). Without seeing the information withing the file itself it is difficult to say. Is the data text or binary? If binary, is it string or character (yes there is a difference).

borelli35


0

Response Number 2
Name: Don Arnett
Date: September 13, 2003 at 22:39:08 Pacific
Reply:

This is an easy and common beginner problem.

Let's walk thru this code, starting at the top of the loop and just after the last record was read and output (so the next read will find the end of file).


while(!f.eof())
{
f.read((char*)&S,sizeof(S));
coutS;
}


- f.eof() will return false because the last read did not find end of file, so the loop continues

- f.read() will attempt to read a record but will find end of file

- the cout outputs the contents of S (which are the contents of the last successful read)

- FINALLY, f.eof() is check, found to be true and then the loop quits.


The problem is that the loop is coded such that there is a cout between the execution of the f.read() and the test of f.eof().


One solution is to do a primer read before the loop and then reorder the two statements inside the loop. You need to check for eof immediately after reading.

f.read(....);
while(!f.eof())
{
cout S;
f.read((char*)&S,sizeof(S));
}


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Problem reading file in c++

Reading files in c++ www.computing.net/answers/programming/reading-files-in-c/9024.html

Writing to a file in C www.computing.net/answers/programming/writing-to-a-file-in-c/10521.html

handling files in c www.computing.net/answers/programming/handling-files-in-c/18818.html