473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 3270
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.