C++ getline and EOF

Steelerz37

Senior member
Feb 15, 2003
693
0
0
Hello,
I am writing a program for my c++ course and am having a problem.
I am creating my own string class, it is call String150. The loop for my getline function looks like this:

while(!fileIn.eof() && ch != '\n' ) //checks for end of file and new line
{
str150 = str150 + ch; //concats a char at a time
fileIn.get(ch);
}

there is a fileIn.get(ch) above the loop as well, just a standard Sentinal controlled loop.

My problem is this. getline will get the line without any problems but when I am returned to main, my loop that is counting the words also tests for EOF, it then skips the loop I need to go into to process the last line. I can change main becuase that was a homework assignment from a while ago, and the String150 class is meant to be a drop in replacement for the standard string class. Any hints would be great. I have looked at the putback function but am unsure if thats what I need to use or even how to implement it.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
I don't quite get the problem? You want to read the contents twice? Why can't you just take the contents out of str150?
 

Steelerz37

Senior member
Feb 15, 2003
693
0
0
No, I dont need to read it twice. the problem is if in my class function, getline() it reads the last line of the file, when i am returned to main, my loop in main checks for eof, and the file pointer is on EOF and I do have the last line filled into my String150 variable, s in main, The sentinal loop finds EOF then skips all the statements in the loop. And like i said, I cant change main at all, so my error has to be corrected in my getline function. Is there a way I can move the stream filepointer to my input file back to the last char in the file? That way in getline() I could just test for eof at the end of my loop and if true, move back one char, return to main, process my loop, then before I would enter my getline loop I get my next char, then it would find EOF, skip the getline() loop and return to main and everything would be correct. I'm sure this isnt explained very well, but hopefully you can get my drift. When I get back to my pc, I will post my loop in main, and my loop in the getline() function and maybe it will be more clear.
 

Steelerz37

Senior member
Feb 15, 2003
693
0
0
In main:

if (! inFile.fail() ) //Tests to make sure the file opened correctly
{
getline(inFile, s);
while (!inFile.eof())
{
cout << s << endl;
wordCount = wordCount + countWords(s);
getline(inFile, s);
}
inFile.close();


In getline() function

//2.0 Process
fileIn.get(ch);
//Sentinal
while(!fileIn.eof() &amp;&amp; ch != '\n' ) //checks for end of file and new line
{
str150 = str150 + ch; //concats a char at a time
fileIn.get(ch);
}


Edit:

I've found if I add this after my loop in getline()

if(fileIn.eof())
fileIn.clear();

it has the desired results, but it throws me into an infinte loop, I'm unsure of where I can put this code so it will only excecute the first time eof is hit
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
So you need to return 2 pieces of information from a function:
1. whether the function read a line or not
2. the line itself

So you can either pass 2 arguments ( linebuffer, bool&amp; read_anything_good_lately ) or 1 argument and send back the bool / int status as the function's return value. Returning a status code is very common for functions so that's probably the right approach.
 

Steelerz37

Senior member
Feb 15, 2003
693
0
0
I could do that, but the requirements are that main, a word counter program, must be able to work interchangebly with the standard string class and my String150 class, so I really can not pass anything back or modify main in anyway. How does the standard string library implement this? maybe I'm missing something and I dont even have to modify the call to getline()?
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Did you write the main() code? If that can be changed, I think you you can fix things just by skipping the read before the while.