|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
|
|
#1
|
|||
|
|||
|
using fstream
I am using fstream to process txt files.
I use a loop that looks like while(inputFile) { processline(inputFile); } 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: { char c; 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? |
|
#2
|
|||
|
|||
|
!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...
__________________
-- Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more. |
|
#3
|
|||
|
|||
|
when reading to the end of a file you need something like this
int main() { ifstream infile; infile.open("whatever.txt."); infile >> (random variable); while(infile) { do some stuff infile >> (random variable again); } } |
|
#4
|
|||
|
|||
|
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: while(ins.ReadOneRecord(DataFile)) /*ReadOneRecord is a member function*/ { ins.WriteReortDetailLine(ReportFile); } 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) { //Declare Variables char tempTotalSales [7]; char temp TotalCommission [6]; char eol; //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 DataFile.getline(employeeNum,5) 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. DataFile.getline(tempTotalSales,7); DataFile.getline(tempTotalCommision,6); DataFile.get(eol); //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: totalSales=atoi(tempTotalSales); return true; } 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 |
|
#5
|
|||
|
|||
|
to explain further...
I use a loop that looks like while(!inputFile.eof()) { processline(inputFile); } 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). |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > using fstream |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|