By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,955 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,955 IT Pros & Developers. It's quick & easy.

Check if program finished to read a whole file

P: n/a
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 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
lovecreatesbea...@gmail.com 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
Jan 23 '07 #2

P: n/a

Victor Bazarov wrote:
lovecreatesbea...@gmail.com 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. Josuttisand other books.

Jan 23 '07 #3

P: n/a

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 '07 #4

P: n/a
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 '07 #5

P: n/a
On 2007-01-23 18:12, lovecreatesbea...@gmail.com 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
Jan 23 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.