February 1st, 2003, 11:31 AM
I am using fstream to process txt files.
I use a loop that looks like
It all works fine except that the loop does not end at the end of the file. It decides to go around again, and then my program crashes.
when i put in these lines into the loop:
c = inputFile.peek();
cout << "\n" << c;
/* As well as: */ processline(inputFile);
it will print out the symbol y(with an umlaut over it) as the character it has peeked when there should be no character to peek.
I have also used (!inputFile.eof()) as the guard, but it has the same results.
Does anyone know why my loop doesnt stop at what I would think is the end of the file?
February 1st, 2003, 03:40 PM
!inputFile.eof() is the right one.
if you test "inputFile", it will run as long as opening the file didnīt fail and read over the end.
but without more of your code, i canīt tell why it does not work for you. your code snippet does not read from the stream. so it would loop forever...
February 1st, 2003, 04:34 PM
when reading to the end of a file you need something like this
infile >> (random variable);
do some stuff
infile >> (random variable again);
February 1st, 2003, 11:59 PM
I kinda of have an idea of what I think your trying to do. I am assuming your text file has multiple records or line. but first things first.
when writing a record to a text file you have to convert numeric to stirng using atoi or atof etc function but back to your probelm here is a sample code I have that might help.
this a loop that reads a record from a text file and writes it to a report file:
/*ReadOneRecord is a member function*/
The following code is what I have in a header file but can be used where ever you define your functiont to write/read your txt file
bool Insure:: ReadOneRecord(ifstream& DataFile)
char tempTotalSales ;
char temp TotalCommission ;
//These are tempory character arrays that will be used to convert numeric into string the eol tell when it has reached the end of a record and move to the next one the array size is the field size plus 1
//Read One Record
if(DataFile.eof()) return false;
//Then continue to add the rest of your variables below using the same syntex as the one for employeeNum also remember the second argument is the field size.
//You can not use .getline for the eol (or end of your record). also when you are reading a record you must read them in the order they appear or it could cause problems, so code in in the order they are.
Now if you need to convert the temp arrays here is the code to do it:
Now this probably more code then you ask, but your post was very vague so I inlcuded the converting numbers to string in case you run into that probelm but the key thing to tell if your loop when you reached the end of a file is the if statement and the fact that the function datatype is bool meaning it will return a true or false value and if it is false it will return false and exit the loop signifining it is the end of file. hope this helps any
February 2nd, 2003, 12:30 PM
to explain further...
I use a loop that looks like
where the processline is a member function:
void process_line(ifstream& inptFile)
and inside the funtion process_line uses the fstream in the loop:
while ((inptFile >> strng) && (strng!=":"))
title = title + " " + strng;
and the call:
getline(inptFile, tgpth, '\n');
the whole thing works if instead of a while loop , i just put in a for loop for the amount of lines i need to process. This soloution is obviously a very temporary one, as the program will not know how many lines it needs to process.
As far as i can see the while loop should work, but it doesnt. Its almost like it loops once more before it stops. but then again maybe it would never stop, i dont know.
Is it something to do with the way I pass the stream a parameter to another method(i.e. the process line one).