filestream problem

Discussion in 'C++' started by Pieter Provoost, Jun 15, 2005.

  1. I'm having a little filestream problem. With the code below, I open a file
    in order to insert its contents into another file. However, right after the
    opening of the file, insertfile.eof() seems to be TRUE. Before the
    problematic file is opened, one other file is opened and inserted by the
    same piece of code (which is in a loop), without any problems. I'm sure that
    the file to be inserted has several lines of contents, and I also checked if
    the insertfile stream is closed before opening it.

    if(FileExists(insertfilepath))
    {
    insertfile.open(insertfilepath.c_str());
    while(! insertfile.eof())
    {
    getline(insertfile, insertline);
    outfile << insertline << endl;
    }
    insertfile.close();
    }

    I would really appreciate some advise here.

    Thanks,
    Pieter
     
    Pieter Provoost, Jun 15, 2005
    #1
    1. Advertising

  2. Pieter Provoost wrote:
    > I'm having a little filestream problem. With the code below, I open a file
    > in order to insert its contents into another file. However, right after the
    > opening of the file, insertfile.eof() seems to be TRUE. Before the
    > problematic file is opened, one other file is opened and inserted by the
    > same piece of code (which is in a loop), without any problems. I'm sure that
    > the file to be inserted has several lines of contents, and I also checked if
    > the insertfile stream is closed before opening it.
    >
    > if(FileExists(insertfilepath))
    > {
    > insertfile.open(insertfilepath.c_str());
    > while(! insertfile.eof())
    > {
    > getline(insertfile, insertline);
    > outfile << insertline << endl;
    > }
    > insertfile.close();


    Add

    insertfile.clear();

    here. The 'eof' bit gets carried over and is not cleared by the 'open'
    operation, most likely.

    > }
    >
    > I would really appreciate some advise here.


    HTH

    V
     
    Victor Bazarov, Jun 15, 2005
    #2
    1. Advertising

  3. "Victor Bazarov" <> schreef in bericht
    news:YaXre.1625$01.us.to.verio.net...
    > Pieter Provoost wrote:
    > > I'm having a little filestream problem. With the code below, I open a

    file
    > > in order to insert its contents into another file. However, right after

    the
    > > opening of the file, insertfile.eof() seems to be TRUE. Before the
    > > problematic file is opened, one other file is opened and inserted by the
    > > same piece of code (which is in a loop), without any problems. I'm sure

    that
    > > the file to be inserted has several lines of contents, and I also

    checked if
    > > the insertfile stream is closed before opening it.
    > >
    > > if(FileExists(insertfilepath))
    > > {
    > > insertfile.open(insertfilepath.c_str());
    > > while(! insertfile.eof())
    > > {
    > > getline(insertfile, insertline);
    > > outfile << insertline << endl;
    > > }
    > > insertfile.close();

    >
    > Add
    >
    > insertfile.clear();
    >
    > here. The 'eof' bit gets carried over and is not cleared by the 'open'
    > operation, most likely.



    Works fine, thanks! I forgot to mention that I'm using BCB6, I don't know if
    this problem also occurs with other compilers...

    Pieter
     
    Pieter Provoost, Jun 15, 2005
    #3
  4. Pieter Provoost wrote:
    >
    > I'm having a little filestream problem. With the code below, I open a file
    > in order to insert its contents into another file. However, right after the
    > opening of the file, insertfile.eof() seems to be TRUE. Before the
    > problematic file is opened, one other file is opened and inserted by the
    > same piece of code (which is in a loop), without any problems. I'm sure that
    > the file to be inserted has several lines of contents, and I also checked if
    > the insertfile stream is closed before opening it.
    >
    > if(FileExists(insertfilepath))
    > {
    > insertfile.open(insertfilepath.c_str());
    > while(! insertfile.eof())
    > {
    > getline(insertfile, insertline);
    > outfile << insertline << endl;
    > }
    > insertfile.close();
    > }
    >
    > I would really appreciate some advise here.
    >


    you are using eof() in the wrong way
    (I am just waiting for your next posting entitled:
    "why does the last line of my file get processed twice?")

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jun 15, 2005
    #4
  5. Pieter Provoost wrote:
    > "Victor Bazarov" <> schreef in bericht
    > news:YaXre.1625$01.us.to.verio.net...
    > [..] The 'eof' bit gets carried over and is not cleared by the 'open'
    >>operation, most likely.

    >
    >
    >
    > Works fine, thanks! I forgot to mention that I'm using BCB6, I don't know if
    > this problem also occurs with other compilers...


    I have no idea about BCB6. AFAICT, it's standard behaviour.

    V
     
    Victor Bazarov, Jun 15, 2005
    #5
  6. "Karl Heinz Buchegger" <> schreef in bericht
    news:...
    > Pieter Provoost wrote:
    > >
    > > I'm having a little filestream problem. With the code below, I open a

    file
    > > in order to insert its contents into another file. However, right after

    the
    > > opening of the file, insertfile.eof() seems to be TRUE. Before the
    > > problematic file is opened, one other file is opened and inserted by the
    > > same piece of code (which is in a loop), without any problems. I'm sure

    that
    > > the file to be inserted has several lines of contents, and I also

    checked if
    > > the insertfile stream is closed before opening it.
    > >
    > > if(FileExists(insertfilepath))
    > > {
    > > insertfile.open(insertfilepath.c_str());
    > > while(! insertfile.eof())
    > > {
    > > getline(insertfile, insertline);
    > > outfile << insertline << endl;
    > > }
    > > insertfile.close();
    > > }
    > >
    > > I would really appreciate some advise here.
    > >

    >
    > you are using eof() in the wrong way
    > (I am just waiting for your next posting entitled:
    > "why does the last line of my file get processed twice?")
    >
    > --
    > Karl Heinz Buchegger



    "I would really appreciate some advise here."
    The last line is not processed twice, btw.

    Pieter
     
    Pieter Provoost, Jun 16, 2005
    #6
  7. Pieter Provoost wrote:
    >
    > >
    > > you are using eof() in the wrong way
    > > (I am just waiting for your next posting entitled:
    > > "why does the last line of my file get processed twice?")
    > >
    > > --
    > > Karl Heinz Buchegger

    >
    > "I would really appreciate some advise here."


    See below

    > The last line is not processed twice, btw.


    It is. You just haven't noticed it :)
    Compare the input file closely with the output file and you will
    see that the last line is duplicated. In case the last line is an empty
    line, you will find, that your output file has one empty line more then
    the input file. It is easy to miss, but it is there. If you don't
    believe me, make an experiment: In your input file, in the last line,
    write some text but don't terminate that line with a 'return'. Then
    you will see.

    The point is:
    eof() turns to true only after you tried *and failed* to read past
    the end of file. C++ has no crystal ball, like other languages. It doesn't
    try to predict the future.

    eof() is ment to be used to figure out why a preceeding read operation
    has failed. So the typical read loop in C++ looks like this

    while( read operation works ) {
    do something with the read data
    }

    // read operation has failed, but why?
    // use eof() to figure out if it was because of EOF
    // if it was, then the file was processed entirely.
    // if it was not, then something else has happened

    if( eof does not return true ) {
    alert user of file reading error and take
    some actions
    }

    All of the C++ read operations support this by returning something
    that can immediatly be used for checking if the operation has succeeded.
    In your case:

    while( getline( insertfile, insertline ) )
    outfile << insertline << endl;

    if( !insertfile.eof() )
    cout << "Error during file copy operation";

    This also has the advantage, that the reading loop terminates not only
    because of eof, but also because of all sorts of errors that can happen
    during reading: file corrupt, network connection breakdown, user removing
    the floppy from the drive, hard disc failure, etc...

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jun 16, 2005
    #7
  8. "Karl Heinz Buchegger" <> schreef in bericht
    news:...
    > Pieter Provoost wrote:
    > >
    > > >
    > > > you are using eof() in the wrong way
    > > > (I am just waiting for your next posting entitled:
    > > > "why does the last line of my file get processed twice?")
    > > >
    > > > --
    > > > Karl Heinz Buchegger

    > >
    > > "I would really appreciate some advise here."

    >
    > See below
    >
    > > The last line is not processed twice, btw.

    >
    > It is. You just haven't noticed it :)
    > Compare the input file closely with the output file and you will
    > see that the last line is duplicated. In case the last line is an empty
    > line, you will find, that your output file has one empty line more then
    > the input file. It is easy to miss, but it is there. If you don't
    > believe me, make an experiment: In your input file, in the last line,
    > write some text but don't terminate that line with a 'return'. Then
    > you will see.
    >
    > The point is:
    > eof() turns to true only after you tried *and failed* to read past
    > the end of file. C++ has no crystal ball, like other languages. It doesn't
    > try to predict the future.
    >
    > eof() is ment to be used to figure out why a preceeding read operation
    > has failed. So the typical read loop in C++ looks like this
    >
    > while( read operation works ) {
    > do something with the read data
    > }
    >
    > // read operation has failed, but why?
    > // use eof() to figure out if it was because of EOF
    > // if it was, then the file was processed entirely.
    > // if it was not, then something else has happened
    >
    > if( eof does not return true ) {
    > alert user of file reading error and take
    > some actions
    > }
    >
    > All of the C++ read operations support this by returning something
    > that can immediatly be used for checking if the operation has succeeded.
    > In your case:
    >
    > while( getline( insertfile, insertline ) )
    > outfile << insertline << endl;
    >
    > if( !insertfile.eof() )
    > cout << "Error during file copy operation";
    >
    > This also has the advantage, that the reading loop terminates not only
    > because of eof, but also because of all sorts of errors that can happen
    > during reading: file corrupt, network connection breakdown, user removing
    > the floppy from the drive, hard disc failure, etc...
    >
    > --
    > Karl Heinz Buchegger
    >


    Thanks a lot for your reply, I will have a very close look at it. I did the
    experiment as you described it before, and in the new file the last line was
    not duplicated. But I'm sure you are right if you say I'm using eof() the
    wrong way, I'm just starting with C++. I'll rewrite that piece of code...

    Cheers
    Pieter
     
    Pieter Provoost, Jun 16, 2005
    #8
  9. Pieter Provoost wrote:
    >
    > not duplicated. But I'm sure you are right if you say I'm using eof() the
    > wrong way, I'm just starting with C++. I'll rewrite that piece of code...


    Wee see this all to often.
    Even book authors get it wrong :)


    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jun 16, 2005
    #9
  10. Karl Heinz Buchegger wrote:
    > Pieter Provoost wrote:
    >
    >>not duplicated. But I'm sure you are right if you say I'm using eof() the
    >>wrong way, I'm just starting with C++. I'll rewrite that piece of code...

    >
    >
    > Wee see this all to often.
    > Even book authors get it wrong :)


    Book authors are people, aren't they? I mean, most often, that is. :)
     
    Victor Bazarov, Jun 16, 2005
    #10
  11. Pieter Provoost

    Panjandrum Guest

    Karl Heinz Buchegger wrote:
    > Wee see this all to often.
    > Even book authors get it wrong :)


    Then the library interface is not well designed and the library should
    be avoided (at least for real work with files).
     
    Panjandrum, Jun 16, 2005
    #11
    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. =?Utf-8?B?TWlrYWVsIEd1c3RhZnNzb24=?=

    FileStream permission problem with asp.NET web application!

    =?Utf-8?B?TWlrYWVsIEd1c3RhZnNzb24=?=, May 26, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    4,880
    Guest
    May 28, 2004
  2. Nathan Sokalski

    Problem creating a Bitmap from an IO.FileStream

    Nathan Sokalski, Dec 10, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    5,330
    Miha Markic [MVP C#]
    Dec 13, 2005
  3. Tim Müller
    Replies:
    2
    Views:
    2,186
    Tim Müller
    Feb 5, 2007
  4. Radek
    Replies:
    4
    Views:
    1,330
  5. Frank

    FileStream file name problem.

    Frank, May 7, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    284
    Frank
    May 7, 2008
Loading...

Share This Page