Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > fstream File i/o

Reply
Thread Tools

fstream File i/o

 
 
Mike Copeland
Guest
Posts: n/a
 
      05-15-2012
I am trying to convert some applications to be "more C++" and use
safer I/o. The code below works, but it poses an annoyance: the last
record "read" from the input file has no content. Therefore, I must
include the "if(line.length())" code before writing any data to the
output file, because failing to do that adds a zero-length record to the
end of the output file.
What am I missing in this logic that would avoid adding the code?
TIA

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
string line;
fstream f1, f2;
f1.open("pat12.n00", fstream::in);
if(f1.is_open())
{
f2.open("test.txt", fstream:ut);
do
{
getline(f1, line);
if(line.length()) f2 << line << endl;
} while(!f1.eof());
f1.close();
f2.close();
}
else cout << "Unable to open file";

return 0;
}
 
Reply With Quote
 
 
 
 
Joshua Maurice
Guest
Posts: n/a
 
      05-16-2012
On May 15, 4:53*pm, (E-Mail Removed) (Mike Copeland) wrote:
> * *I am trying to convert some applications to be "more C++" and use
> safer I/o. *The code below works, but it poses an annoyance: the last
> record "read" from the input file has no content. *Therefore, I must
> include the "if(line.length())" code before writing any data to the
> output file, because failing to do that adds a zero-length record to the
> end of the output file.
> * *What am I missing in this logic that would avoid adding the code?
> TIA
>
> #include <iostream>
> #include <fstream>
> #include <string>
> using namespace std;
>
> int main ()
> {
> * string * line;
> * fstream *f1, f2;
> * f1.open("pat12.n00", fstream::in);
> * if(f1.is_open())
> * {
> * * f2.open("test.txt", fstream:ut);
> * * do
> * * {
> * * * getline(f1, line);
> * * * if(line.length()) f2 << line << endl;
> * * } while(!f1.eof());
> * * f1.close();
> * * f2.close();
> * }
> * else cout << "Unable to open file";
>
> * return 0;
> }


You must check whether the read succeeded before using the results of
the read. The idiomatic way is:
string line;
while (getline(f1, line))
{
//use line
}

".eof()" returns when eof has been reached. A getline can successfully
read characters into "line" and set "eof".

Short version: You should check the "fail" flag, which is set when no
further characters can be read, such as when eof is reached and no
characters were read in this operation before eof was reached. The
(effective) implicit conversion to bool in "while (getline(f1, line))"
does a check of the "fail" flag after doing the read operation.

Check your favorite C++ reference for the full semantics.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
fstream data from file, with file periodically written with new data roughtrader C++ 3 02-17-2009 12:42 PM
what is different between <fstream.h> and <fstream>MS VC++ Armando C++ 6 01-29-2004 09:01 AM
File associated with fstream object? Sanyi Benczik C++ 2 01-21-2004 12:05 PM
file creation problem in Windows using fstream Brandon McCombs C++ 8 11-24-2003 08:46 AM
fstream file i/o Dave C++ 2 08-04-2003 09:56 PM



Advertisments