GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Apr-2004, 21:21
stevies24 stevies24 is offline
New Member
 
Join Date: Apr 2004
Posts: 4
stevies24 is on a distinguished road

reading a file twice


Hi, I am writting a program that needs to read a file once, do some calculations, then read it again from the top and perform some new calculations. I have been able to read the file the first time using
CPP / C++ / C Code:
while(!feof(file)) 
and then tried to read it again with a similar loop later in the program. The second time it didn't read the file. Am I going to have to close and re-open the file in between reads or is there are more sensible way?

Thanks
stevies24
  #2  
Old 11-Apr-2004, 21:30
sdeming sdeming is offline
VIP
 
Join Date: May 2003
Location: Michigan, US
Posts: 8
sdeming will become famous soon enough
You should use the rewind() function immediately before your second loop:
CPP / C++ / C Code:
rewind(file);
while (!feof(file)) { ... }
  #3  
Old 11-Apr-2004, 22:56
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by stevies24
Hi, I am writting a program that needs to read a file once, do some calculations, then read it again from the top and perform some new calculations. I have been able to read the file the first time using
CPP / C++ / C Code:
while(!feof(file)) 
and then tried to read it again with a similar loop later in the program. The second time it didn't read the file. Am I going to have to close and re-open the file in between reads or is there are more sensible way?

Thanks
stevies24
Well you're lucky. The feof() function should not be used in loops. See this link in the tutorial for an explanation.

As sdeming said, use rewind(). Or close and reopen the file as you surmised. Either is acceptable.
__________________

Age is unimportant -- except in cheese
  #4  
Old 14-Apr-2004, 17:31
stevies24 stevies24 is offline
New Member
 
Join Date: Apr 2004
Posts: 4
stevies24 is on a distinguished road
Quote:
Originally Posted by WaltP
Well you're lucky. The feof() function should not be used in loops. See this link in the www.gidforums.com for an explanation.

As sdeming said, use rewind(). Or close and reopen the file as you surmised. Either is acceptable.

Ok, thanks for your help. I've got it worked using the code in the tutorial and the review() function.

Thanks
 
 

Recent GIDBlogMeeting the populace by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading from file jmenendezr C++ Forum 2 26-Feb-2006 16:00
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28
Re: Programming Techniques WaltP C Programming Language 0 09-Mar-2004 23:56
reading and writing to a file in C++ mgp6q C++ Forum 17 02-Mar-2004 12:42

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:35.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.