Check if program finished to read a whole file

Discussion in 'C++' started by lovecreatesbea...@gmail.com, Jan 23, 2007.

  1. Guest

    The condition at line 31 is added to check if the program finished to
    read the whole file. Is it needed and correct? Thank you.

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

    int read(string filename, int last_pos)
    {
    ifstream file;
    int size = 0;
    //read continue after the last read position
    static int offset = last_pos + 1;

    file.open(filename.c_str(), ios::in);
    if(!file)
    {
    cerr << "failed to open file: " << filename << endl;
    return -1;
    }

    file.seekg(0, ios::end);
    size = file.tellg();
    file.seekg(offset, ios::beg);
    string line;
    while (getline(file, line))
    {
    //...
    offset = file.tellg();
    }

    //check if it finished to read the whole file
    if (offset != size) // line 31
    {
    file.clear();
    read(filename, offset);
    }

    return 0;
    }
    , Jan 23, 2007
    #1
    1. Advertising

  2. wrote:
    > The condition at line 31 is added to check if the program finished to
    > read the whole file. Is it needed and correct? Thank you.
    >
    > #include <fstream>
    > #include <iostream>
    > #include <string>
    > using namespace std;
    >
    > int read(string filename, int last_pos)


    Consider passing 'filename' string by a reference to const.

    > {
    > ifstream file;
    > int size = 0;
    > //read continue after the last read position
    > static int offset = last_pos + 1;
    >
    > file.open(filename.c_str(), ios::in);
    > if(!file)
    > {
    > cerr << "failed to open file: " << filename << endl;
    > return -1;
    > }
    >
    > file.seekg(0, ios::end);
    > size = file.tellg();
    > file.seekg(offset, ios::beg);
    > string line;
    > while (getline(file, line))
    > {
    > //...
    > offset = file.tellg();
    > }
    >
    > //check if it finished to read the whole file
    > if (offset != size) // line 31
    > {
    > file.clear();
    > read(filename, offset);


    Here you seem to be recursing. That means the file is reopened
    in that function. I don't think it's a good idea.

    > }
    >
    > return 0;
    > }


    When 'getline' attempts to read beyond the last byte in the file,
    the 'file.eof()' will return true. Perhaps you should use that
    instead of checking the position... What book are you reading on
    C++ I/O?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jan 23, 2007
    #2
    1. Advertising

  3. Guest

    Victor Bazarov wrote:
    > wrote:
    > > The condition at line 31 is added to check if the program finished to
    > > read the whole file. Is it needed and correct? Thank you.
    > >
    > > #include <fstream>
    > > #include <iostream>
    > > #include <string>
    > > using namespace std;
    > >
    > > int read(string filename, int last_pos)

    >
    > Consider passing 'filename' string by a reference to const.
    >
    > > {
    > > ifstream file;
    > > int size = 0;
    > > //read continue after the last read position
    > > static int offset = last_pos + 1;
    > >
    > > file.open(filename.c_str(), ios::in);
    > > if(!file)
    > > {
    > > cerr << "failed to open file: " << filename << endl;
    > > return -1;
    > > }
    > >
    > > file.seekg(0, ios::end);
    > > size = file.tellg();
    > > file.seekg(offset, ios::beg);
    > > string line;
    > > while (getline(file, line))
    > > {
    > > //...
    > > offset = file.tellg();
    > > }
    > >
    > > //check if it finished to read the whole file
    > > if (offset != size) // line 31
    > > {
    > > file.clear();
    > > read(filename, offset);

    >
    > Here you seem to be recursing. That means the file is reopened
    > in that function. I don't think it's a good idea.


    Thank you. Does a close() call solve this problem as shown below?

    if (offset != size) // line 31
    {
    file.clear();
    file.close(); //close the file. try to re-read the rest again.
    read(filename, offset);
    ...

    >
    > > }
    > >
    > > return 0;
    > > }

    >
    > When 'getline' attempts to read beyond the last byte in the file,
    > the 'file.eof()' will return true. Perhaps you should use that
    > instead of checking the position... What book are you reading on
    > C++ I/O?


    Yes, I read <The C++ Programming Language, special ed>, <C++ Primer,
    4th>, <The C++ Standard Library by Nicolai M. Josuttis> and other books.
    , Jan 23, 2007
    #3
  4. Guest

    Victor Bazarov wrote:
    > When 'getline' attempts to read beyond the last byte in the file,
    > the 'file.eof()' will return true. Perhaps you should use that
    > instead of checking the position.


    I will try to use the eof utility, thank you.
    , Jan 23, 2007
    #4
  5. Guest

    Victor Bazarov wrote:
    > When 'getline' attempts to read beyond the last byte in the file,
    > the 'file.eof()' will return true. Perhaps you should use that
    > instead of checking the position.


    But one can't know whether getline() fails before reaching the end of
    the file. Comparing the last read position and the file size tells a
    file has been read tiil the end or not, right?
    , Jan 23, 2007
    #5
  6. On 2007-01-23 18:12, wrote:
    > Victor Bazarov wrote:
    >> When 'getline' attempts to read beyond the last byte in the file,
    >> the 'file.eof()' will return true. Perhaps you should use that
    >> instead of checking the position.

    >
    > But one can't know whether getline() fails before reaching the end of
    > the file. Comparing the last read position and the file size tells a
    > file has been read tiil the end or not, right?


    If readline fails (the loop terminates) it can only be of two reasons,
    either EOF is reached (in which case eof() == true) or because of a
    failure (in which case eof() == false, but fail() == true) so it's
    enough to check eof().

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jan 23, 2007
    #6
    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. Blankdraw
    Replies:
    3
    Views:
    385
    Pieter Droogendijk
    Aug 10, 2003
  2. \A_Michigan_User\
    Replies:
    2
    Views:
    881
    \A_Michigan_User\
    Aug 21, 2006
  3. Holger
    Replies:
    1
    Views:
    346
    Holger
    Nov 7, 2008
  4. Sreejith K
    Replies:
    24
    Views:
    1,011
    Sreejith K
    Mar 24, 2009
  5. Steven D'Aprano

    How to check for threads being finished?

    Steven D'Aprano, Jul 5, 2013, in forum: Python
    Replies:
    7
    Views:
    82
    Stefan Behnel
    Jul 6, 2013
Loading...

Share This Page