C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old February 1st, 2003, 10:31 AM
gamgee gamgee is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Ireland
Posts: 26 gamgee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old February 1st, 2003, 02:40 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
!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.

Reply With Quote
  #3  
Old February 1st, 2003, 03:34 PM
pschmerg pschmerg is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Blacksburg VA/Philly PA
Posts: 38 pschmerg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 29 m
Reputation Power: 6
Send a message via AIM to pschmerg
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);
}
}

Reply With Quote
  #4  
Old February 1st, 2003, 10:59 PM
CStrauss CStrauss is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 37 CStrauss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 3 m 50 sec
Reputation Power: 6
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

Reply With Quote
  #5  
Old February 2nd, 2003, 11:30 AM
gamgee gamgee is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Ireland
Posts: 26 gamgee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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).

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > using fstream


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway