Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > streambuffs and their associated buffers

Reply
Thread Tools

streambuffs and their associated buffers

 
 
drmario
Guest
Posts: n/a
 
      03-26-2008
I have a program that opens a text file, reads data in (using
exclusively ifstream::get(), ifstream:eek(), ifstream::ignore(),
ifstream::clear(), and ifstream& operator >>), crunch some #s, output data,
close the file.

Is there a function I can use to directly access the buffer associated with
the streambuff of my ifstream object?

I'm getting input failure and I can't figure out why. So I wanna go to
the step directly before the failure, and then say "show me everything
currently in the input buffer". I know, I know I can fix this by just
changing my algorithm. I don't want to. I want to know why what I'm doing
isn't working, and fix it. Then I'll change it If you like Windows
more than UNIX you may be asking yourself "Why the hell would you wanna know
that, instead of just changing it?" lol.

Thanks in advance,
Mario



 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      03-26-2008
On 26 mar, 20:30, "drmario" <(E-Mail Removed)> wrote:
> I have a program that opens a text file, reads data in (using
> exclusively ifstream::get(), ifstream:eek(),
> ifstream::ignore(), ifstream::clear(), and ifstream& operator
> >>), crunch some #s, output data, close the file.


> Is there a function I can use to directly access the buffer
> associated with the streambuff of my ifstream object?


Try rdbuf(). It should return a pointer to the streambuf object
(or the filebuf object, if you call it on an ifstream, rather
than just on an istream).

> I'm getting input failure and I can't figure out why. So I
> wanna go to the step directly before the failure, and then say
> "show me everything currently in the input buffer".


Input failure isn't necessarily from the streambuf. It can be a
result of a error in the input format, for example. And...
regretfully, even getting at the filebuf won't provide you any
information. (A flaw in the standard I/O, IMHO.)

For starters, even before worrying about that level: Check the
status after every input, and as soon as you fail, check eofbit,
i.e.:

source >> something ;
if ( ! source ) {
if ( source.bad() ) {
// You've got a better implementation than most:
// there was a hard error somewhere. (On a Unix
// system, try outputting errno here. Might work
// on Windows as well, for that matter.)
} else if ( source.eof() ) {
// This generally means that the filebuf reported
// an error (or EOF).
} else {
// The input wasn't formatted correctly for what
// you were trying to read.
}
}

Once you've isolated the exact input which failed, try "peek"ing
at the input before the statement, to determine where you are in
the stream.

If all else fails: see
http://kanze.james.neuf.fr/articles/fltrsbf1.html, and use the
information there to write a filtering streambuf which traces
the exact bytes you return (to the istream). But it's more work
than should be necessary.

> I know, I know I can fix this by just changing my algorithm.
> I don't want to. I want to know why what I'm doing isn't
> working, and fix it. Then I'll change it


I definitly approve of trying to understand why something isn't
working before trying to fix it. Otherwise, the new code is
likely to have the same errors (maybe positionned a little
differently, so they don't show up immediately in your tests).

--
James Kanze (GABI Software) email:(E-Mail Removed)
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If Brit police are terrified their jobs are threatened by cutbacks,this isn't the way to illustrate their value RichA Digital Photography 62 01-12-2011 01:10 AM
Prophets are honored by everyone, except the people of their hometownand their own family. Eljee Digital Photography 8 09-21-2006 05:35 AM
Since MSN CHAT went pay per use. Is their any other free ones out their Hugh Computer Support 8 05-19-2004 05:52 PM
What the pros use to power their flashes... and their digital cameras. Dan Sullivan Digital Photography 21 01-04-2004 04:40 PM
Stop Spammers by Hitting Their Servers - Not Their Email. Magic347 Computer Support 27 07-03-2003 04:36 PM



Advertisments