fstream File i/o

Discussion in 'C++' started by Mike Copeland, May 16, 2012.

  1. 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::eek: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;
    }
     
    Mike Copeland, May 16, 2012
    #1
    1. Advertising

  2. On May 15, 4:53 pm, (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::eek: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.
     
    Joshua Maurice, May 16, 2012
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave

    fstream file i/o

    Dave, Aug 4, 2003, in forum: C++
    Replies:
    2
    Views:
    373
    Mike Wahler
    Aug 4, 2003
  2. Brandon McCombs
    Replies:
    8
    Views:
    484
    Dave O'Hearn
    Nov 24, 2003
  3. Sanyi Benczik

    File associated with fstream object?

    Sanyi Benczik, Jan 20, 2004, in forum: C++
    Replies:
    2
    Views:
    420
    Dietmar Kuehl
    Jan 21, 2004
  4. Armando
    Replies:
    6
    Views:
    749
    Armando
    Jan 29, 2004
  5. roughtrader
    Replies:
    3
    Views:
    444
    James Kanze
    Feb 17, 2009
Loading...

Share This Page