473,569 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ifstream eof not reporting eof?

I just did a loop

ifstream i(myfile, ios::binary);

while (!i.eof())
{
i.read(buff, 2);
}

Well it should have come out of the loop but it tried to do the read
operation again...
Now... I know I should have checked the return of the read, but read
returns an ifstream...
How do I go about checking that the data was actually read and what
the heck is wrong with eof?

Jun 13 '07 #1
10 16831
On Jun 13, 11:17 am, SpreadTooThin <bjobrie...@gma il.comwrote:
I just did a loop

ifstream i(myfile, ios::binary);

while (!i.eof())
{
i.read(buff, 2);

}

Well it should have come out of the loop but it tried to do the read
operation again...
Now... I know I should have checked the return of the read, but read
returns an ifstream...
How do I go about checking that the data was actually read and what
the heck is wrong with eof?
Reading the fine manual... It seems that eof will only be true after
you have tried to read and failed...
Pardon me but isn't that a bit silly?

Jun 13 '07 #2
SpreadTooThin wrote:
I just did a loop

ifstream i(myfile, ios::binary);

while (!i.eof())
{
i.read(buff, 2);
}

Well it should have come out of the loop but it tried to do the read
operation again...
Huh?
Now... I know I should have checked the return of the read, but read
returns an ifstream...
So? 'ifstream' has 'good()' member, and it has conversion to 'void*',
so you can do

while (i.read(buff, 2))

or

while (i.read(buff, 2).good())
How do I go about checking that the data was actually read and what
the heck is wrong with eof?
Nothing the heck is wrong with eof. You probably don't understand
how to use it. Try RTFM. Pay specific attention to what flags are
set when 'read' succeeds and/or when it fails, or when it reads less
than requested. Then perform proper actions depending on the flags
you see set after calling 'read'. Also, get a decent book that has
unformatted I/O explained.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 13 '07 #3
On 13 Jun, 18:17, SpreadTooThin <bjobrie...@gma il.comwrote:
I just did a loop

ifstream i(myfile, ios::binary);

while (!i.eof())
{
i.read(buff, 2);

}

Well it should have come out of the loop but it tried to do the read
operation again...
Now... I know I should have checked the return of the read, but read
returns an ifstream...
How do I go about checking that the data was actually read and what
the heck is wrong with eof?
Nothing is wrong with eof as long as you use it in the right way -
which you aren't. Change your code to use eof as described in the FAQ
and see if that fixes your problem.
http://www.parashift.com/c++-faq-lit....html#faq-15.4
http://www.parashift.com/c++-faq-lit....html#faq-15.5

Gavin Deane

Jun 13 '07 #4
SpreadTooThin wrote:
On Jun 13, 11:17 am, SpreadTooThin <bjobrie...@gma il.comwrote:
>I just did a loop

ifstream i(myfile, ios::binary);

while (!i.eof())
{
i.read(buff, 2);

}

Well it should have come out of the loop but it tried to do the read
operation again...
Now... I know I should have checked the return of the read, but read
returns an ifstream...
How do I go about checking that the data was actually read and what
the heck is wrong with eof?

Reading the fine manual... It seems that eof will only be true after
you have tried to read and failed...
Pardon me but isn't that a bit silly?
No. Why is it silly? 'eof' is an error condition that indicates some
specific state of the stream. The 'end-of-file' is not reached upon
a successful read operation. If it's successful, there is no error,
right?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 13 '07 #5
On 13 Jun, 18:22, SpreadTooThin <bjobrie...@gma il.comwrote:
Reading the fine manual... It seems that eof will only be true after
you have tried to read and failed...
Well done. That's the key.
Pardon me but isn't that a bit silly?
Uh.... no, not really. See FAQ 15.5.

Gavin Deane

Jun 13 '07 #6
SpreadTooThin wrote:
\
>
Reading the fine manual... It seems that eof will only be true after
you have tried to read and failed...
Pardon me but isn't that a bit silly?
Nope, that is the way a large plethora of underlying I/O systems
work. EOF isn't detectable unless you try to read something.
C++ takes this as a common denominator.
Jun 14 '07 #7

Ron Natalie <ro*@spamcop.ne twrote in message...
SpreadTooThin wrote:
Reading the fine manual... It seems that eof will only be true after
you have tried to read and failed...
Pardon me but isn't that a bit silly?

Nope, that is the way a large plethora of underlying I/O systems
work. EOF isn't detectable unless you try to read something.
C++ takes this as a common denominator.
Well, you can 'detect' it:

if( MyInFile.peek() == EOF ){/*....*/}

....but the 'peek()' is still a 'read', and that line is seldom usable.
<G>
--
Bob R
POVrookie
Jun 14 '07 #8
On Jun 13, 7:22 pm, SpreadTooThin <bjobrie...@gma il.comwrote:
On Jun 13, 11:17 am, SpreadTooThin <bjobrie...@gma il.comwrote:
I just did a loop
ifstream i(myfile, ios::binary);
while (!i.eof())
{
i.read(buff, 2);
}
Well it should have come out of the loop but it tried to do
the read operation again... Now... I know I should have
checked the return of the read, but read returns an
ifstream... How do I go about checking that the data was
actually read and what the heck is wrong with eof?
Reading the fine manual... It seems that eof will only be true after
you have tried to read and failed...
Pardon me but isn't that a bit silly?
And how do you propose to implement anything else, given the
constraint of also supporting interactive devices, and input of
various types? Suppose the data left in the file is " \n".
What should i.eof() return in the following code:

if ( ! i.eof() ) {
if ( rand() % 1 == 0 ) {
char ch = i.get() ; // guaranteed, since ! i.eof()
} else {
int i ;
i >ch ; // guaranteed, since ! i.eof()
}
}

--
James Kanze (GABI Software, from CAI) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 15 '07 #9
On Jun 13, 7:22 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
SpreadTooThin wrote:
[...]
So? 'ifstream' has 'good()' member, and it has conversion to 'void*',
so you can do
while (i.read(buff, 2))
or
while (i.read(buff, 2).good())
No, no, no, no. There is *never* any time that ios::good()
should be used, since it returns false if eofbit is set (and so
may return false when the read actually succeeds).
How do I go about checking that the data was actually read and what
the heck is wrong with eof?
Nothing the heck is wrong with eof.
Nothing that it would be possible to fix, you mean. A
predictive EOF would in fact be very nice. In the distant past,
some languages (e.g. Pascal) have tried to specify it. In the
end, it never worked correctly; it's nice, but no one knows how
to specify it so that it is both useful and implementable.
You probably don't understand
how to use it. Try RTFM. Pay specific attention to what flags are
set when 'read' succeeds and/or when it fails, or when it reads less
than requested.
And pay specific attention to what flags are tested by each
function as well:-). The basic philosophy of not supporting
predictive EOF is quite justifiable, given the experience of
other languages. The choice and naming of the functions in ios
a lot less so.

--
James Kanze (GABI Software, from CAI) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 15 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13760
by: Dave Johnston | last post by:
Hi, I'm currently trying to create a wrapper that uses C functions but behaves like ifstream (from fstream.h) - this is because the platform I'm using (WinCE) doesn't support streams and this is the easiest way to take a huge project across onto it. Basically, I've hit a problem. I have no idea how the ifstream class handles directories....
7
3226
by: Anton Ishmurzin | last post by:
Greetings All, I think everybodyknows the answer already. But i am quite a newbie in c++. I've got the following line in my code: ifstream ini_file_in("filename.dat", ios::in); But, the gcc (GCC) 3.2 spits out some stuff about
6
3634
by: Herv? LEBAIL | last post by:
Hi everybody, I'm writing a program which use the <string>, <vector> and <ifstream> classes. Given an array of string, i.e vector<string> file_names, example : file_names = "file1.txt" file_names = "file2.txt" etc ...
6
3379
by: Ram Laxman | last post by:
Iam new bie to C++ programming.. I want to write a program which will read the Comma separated values(CSV) file column wise. For example: In a.txt: "TicketNumber","Phone","CarNumber" 10,20,30
4
3727
by: hall | last post by:
Hi. I ran across a bug in one of my problems and after spending some time tracking it down i found that the problem arose in a piece of code that essentially did this: ----------- ifstream in("in.txt"); if(!in) {cout << "error\n";} in.close(); if(!in) {cout << "error\n";} in.close(); if(!in) {cout <<...
10
17929
by: sam | last post by:
Hi, Can anyone tell me how to print a file name from ifstream? the following cout code does not print the filename I created with ifstream preivous: ifstream is; is.open ("text.txt");
1
4872
by: Xiaozhou.Yin | last post by:
Hi~ In the program,I first used the ifstream variable fin open the file and,open it again after called the fin.close().But the fin is fieled to open the file in the second time.The book I'm learning hadn't picked out this.Is it a common problem?Or just occurs in Vc++6.0? #include<fstream> #include<iostream> using namespace std; int...
11
8111
by: adramolek | last post by:
So... I'm trying to get used to using C++ ifstream (or ofstream) instead of stdio (although I'm having second thoughts). Anyways, I want to be able to display a meaningful error message if ifstream fails to open a file, but nothing I read about ifstream says anything about a reliable place to get an error message. Using stdio I can simply do:...
5
7508
by: DrSchwartz | last post by:
Hi there, I have to read in from file, and then process it. It looks like this. #include <iostream> #include <string> #include <fstream> using namespace std; ... int main() {
0
8145
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...
1
7694
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7993
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
6317
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...
0
5245
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
3679
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...
1
2128
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
1
1236
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.