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;
} 5 3305
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
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.
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.
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?
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
prompt asks for a floating number. I am trying to send the program
"3\n" to satisfy the first...
|
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 actiavted
Access 2002 mdb file (and yes, proper rights are set on TMP paths and path,
|
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 the number you
input on the console. It was a very short program, but it took me a
while to write...
|
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?
|
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 switch statement.
thank you for you help
| |
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 do is mostly a reverse engineering
job to get the frame to look right and return the text variable...
|
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?
|
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,
|
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 below. And it works fine except for 2
issues.
First issue: In the Finally of the Try for teh SQL...
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |