473,573 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(filen ame.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(offs et, 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 3291
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(filen ame.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(offs et, 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(filen ame.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(offs et, 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
3572
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 automatically send data to the other program but for some reason the child hangs. The first piece of info prompted for is an integer, and the second...
11
3742
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 2003 Server or ADO or ODBC issue, I am posting this on all of the three newsgroups. That's the setup: Windows 2003 Server with IIS and ASP.NET...
13
3394
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 took me a long time to figure out what I should do. For instance, I was writing a program which tells you all the prime numbers that are less than...
6
4727
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 reading, or writing to that text file until the first user is finished?
2
2109
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 the problem lies when i declare union Allrecords h1,h2,h3. and then use them to toupper record_type. the debug when i use it can not go past the...
9
4409
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 "Run" button, the GUI would close, and the 12 input variables would then be available for the rest of the program. So far, what I have been able to...
9
2838
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 (Array == 'APPLE'){ do something; } or do I need to use a different method to check?
7
2565
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 code before exiting. Is there any way to do this? Any help on this is appreciated :) Regards,
9
2907
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 the text files from the watched folder line by line into variables and then posting them to a SQL table. All of the code for the form is shown...
0
7765
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8000
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8194
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8054
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5578
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5288
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3729
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3729
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.