Check if program finished to read a whole file

L

lovecreatesbea...

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;
}
 
V

Victor Bazarov

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
 
L

lovecreatesbea...

Victor said:
Consider passing 'filename' string by a reference to const.


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);
...
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.
 
L

lovecreatesbea...

Victor said:
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.
 
L

lovecreatesbea...

Victor said:
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?
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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().
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top