473,385 Members | 1,343 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.

question about filebuf and istreams and eof....

I am simply trying to see if I am at the end of a file...
In my class I maintain a filebuf.
In my class I want to provide a method to see if i am at the eof...

My question is can I reconstruct a istream using a filebuf and ask the
istream if it is at eof?
ie...

class myclass
{
filebuf fb;
....
bool eof()
{
istream is(&fb);
return (is.eof());
}
};
Seems like a lot of work to reconstruct the istream on each call to
eof, but then how would I include the istream as a class variable?

Jun 8 '07 #1
1 2296
SpreadTooThin wrote:
I am simply trying to see if I am at the end of a file...
In my class I maintain a filebuf.
In my class I want to provide a method to see if i am at the eof...
You can't. The C++ IO model does not support predictive EOF.

What you can do is try to read.
My question is can I reconstruct a istream using a filebuf and ask the
istream if it is at eof?
ie...
class myclass
{
filebuf fb;
...
bool eof()
{
istream is(&fb);
return (is.eof());
}
};
Seems like a lot of work to reconstruct the istream on each
call to eof, but then how would I include the istream as a
class variable?
Not only a lot of work, but it won't work. Even if fb is at the
end of the file, is.eof() may (and probably will) return false
in the above scenario.

What's wrong with just peeking at the next character, e.g.:

bool eof()
{
return fb.sgetc() == EOF ;
}

It's not reliably repetative, but it will generally work for
real files (not keyboard, etc.), and it is reliable with the
better implementations (both Dinkumware and g++, I think,
although since I also have to contend with some poorer
implementations, it's not something I count on---but maybe I'm
out of date, and this is no longer a problem).

A more reliable solution would be to maintain a flag in your
class, like istream does. Something like:

bool eof()
{
if ( ! isEof ) {
isEof = fb.sgetc() == EOF ;
}
return isEof ;
}

Note that any code actually extracting characters from the
filebuf should alsoset isEof anytime it gets an EOF from sgetc,
sbumpc or snextc.

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

Jun 8 '07 #2

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

Similar topics

3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
1
by: Rob Kreger | last post by:
This code works on Windows XP with VC7.1 stl(sxl 3.08 from dinkumware.) #include "stdafx.h" #include <iostream> #include <stdio.h> #include <io.h> #include <fstream> #include <sys/locking.h>
2
by: Manfred Eckschlager | last post by:
Hi, In Linux C++ there was an easy way to build a stream form a socket like: int sockFd = socket(AF_INET,SOCK_STREAM,0); filebuf *netBuf = new filebuf( sockFd ); -and then iostream...
1
by: Avner | last post by:
I have a C++ code which I want to upgrade to the standard C++ The code constructs a file and sets the file permission The new iostream library does not include filebuf::openprot The old...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
2
by: kelvSYC | last post by:
Suppose I have something like this: class foo { std::istream in; public: foo(std::istream& in_) : in(in_.rdbuf()); void doStuffWith(std::ostream& out); void bar(); };
10
by: edd | last post by:
Hello all, I'm trying to put together some code that reads 3 adjacent pairs of hex digits from an istream. Each hex digit pair represents a non-negative integer. For example I would like to...
3
sangeetha jagannathan
by: sangeetha jagannathan | last post by:
hi can anyone explain me this code, instruction by instruction. i am not able to follow it. #include <iostream.h> #include <fstream.h> #include <stdio.h> #include "logger.h" #include...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.