The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
using fstream
Discuss using fstream in the C Programming forum on Dev Shed. using fstream C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 1st, 2003, 10:31 AM
|
|
Junior Member
|
|
Join Date: Oct 2002
Location: Ireland
Posts: 26
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
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?
|

February 1st, 2003, 02:40 PM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
|
!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, 03:34 PM
|
|
Contributing User
|
|
Join Date: Nov 2002
Location: Blacksburg VA/Philly PA
Posts: 38
Time spent in forums: 2 h 29 m
Reputation Power: 11
|
|
|
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);
}
}
|

February 1st, 2003, 10:59 PM
|
|
Contributing User
|
|
Join Date: Feb 2003
Posts: 37
Time spent in forums: 5 h 3 m 50 sec
Reputation Power: 11
|
|
|
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
|

February 2nd, 2003, 11:30 AM
|
|
Junior Member
|
|
Join Date: Oct 2002
Location: Ireland
Posts: 26
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
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).
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|