www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/10/08/18:44:34

From: dodger AT softhome DOT net (dodger)
Newsgroups: comp.os.msdos.djgpp
Subject: HELP: ifstream object sets eofbit on first get()
Date: Fri, 02 Oct 1998 00:22:33 GMT
Organization: The Matrix
Lines: 58
Message-ID: <361417d2.14017339@news.primenet.com>
Reply-To: dodger AT softhome DOT net
X-Complaints-To: abuse AT globalcenter DOT net
X-Posted-By: @206.165.251.1 (humongous)
X-Newsreader: Forte Free Agent 1.11/32.235
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Okay, I'm a programmer by trade and have had *some* experience with C
and C++, but I've mostly known BASIC.  Now I'm teaching myself C++
from one of the Waite Group's books ("C++ Primer Plus").

I'm not sure what version of DJGPP I've got (I d/l and set up the
compiler earlier this year) but the compile output says GNU version
2.8.1.

Anyway, here's my problem:  I'm writing a utility that will read
through a text document (specifically, an HTML document), find all the
link tags, and output a list of urls.  After writing and debugging the
functions, I moved them all into a class structure (I'm so proud- my
first class :-).  Everything was working fine- I was using cin and
cout to handle I/O and redirection at the command line ( exe >
outputfile < inputfile).  Then I tackled actual file I/O....

To make sure I have the concepts down (as I read my book), I started
by writing the list of urls to an output file.  This worked fine.
Then I went back to cout- so I could see the output on the screen.
Next, I dove into file input:

I declared an input object (this is in the private section of the
class),
	ifstream fin;

pointed it to a filename (this is done in the constructor),
	fin.open(filename);

and started reading the file in.  Here's where I have the problem:

bool html_parse::findtag() {
	char ch;
	while(!fin.eof()) {
		fin.get(ch);
		if (ch == '<') {
			return(true);
		}

	}
	return(false);
}


Since I can't step through the code, I've set up cerr outputs to tell
me what the value of fin.eof() is.  Immediately before fin.get(ch),
the value of the eofbit is false, but immediately after, it is true.
This, of course results in only the first character being read in and
then the program aborting (it *is* getting the correct character, I've
checked that).

Why is the eofbit being set?  What am I missing here?  As I said, this
exact code worked fine when I was using cin instead of fin- all I did
was a search and replace, replacing all instances of cin with fin.

I'd appreciate any advice or insight anybody could offer.

- Shawn

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019