472,374 Members | 1,283 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

Check if program finished to read a whole file

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
5 3158
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

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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chad | last post by:
I need my java program to exec an external .exe file, but the problem is that this program being exec()'ed prompts the user for two pieces of information. I am trying to get my java program to...
11
by: Wolfgang Kaml | last post by:
Hello All, I have been working on this for almost a week now and I haven't anything up my sleeves anymore that I could test in addition or change.... Since I am not sure, if this is a Windows...
13
by: takashi | last post by:
Hi, I have a question. I am learning about how to use c++ language. I have attempted to make my own programs, using the knowledge that I have, but sometimes when I get stuck on writing a code, it...
6
by: joethis | last post by:
Is there a way to make sure that a file is already in use using asp? For instance, if one person has opened a file and is about to write to it; then is there a way to keep another user from...
2
by: JasBascom | last post by:
I thought it was too much to put this program in with my last message. The program compiles ok, but when I execute, I get access errors. can someone put it through their compiler please. I think...
9
by: Tyler | last post by:
Hello All: I am currently working on a project to create an FEM model for school. I was thinking about using wxPython to gather the 12 input variables from the user, then, after pressing the...
9
by: chutsu | last post by:
hi I got a simple program, and I was wondering how do you check if the string in an array = a string. For example if I put "APPLE" in array Array then how can I check it with a if statement. if...
7
by: Vishal | last post by:
Hi, I am writing a CGI to serve files to the caller. I was wondering if there is any way to tell in my CGI if the client browser is still connected. If it is not, i want to execute some special...
9
by: Keith G Hicks | last post by:
I'm having a lot of trouble with "file in use" errors in my "folder watcher" project. Starting and stopping the watcher and reading my XML file work fine. Once the watcher is started, I'm reading...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.