LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-16-2010, 01:30 AM   #1
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Rep: Reputation: 15
C - How do I return to the beginning of a file I'm reading?


I'm reading a text file with fscanf using a loop until feof(inFile). How can I return to the top of the file? As in I have one loop that scans until the eof and then after it there's another loop and I want to start from the beginning of the file again scanning to the end of it. How do I get back there?

Thanks!
 
Old 12-16-2010, 01:36 AM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
You can reposition in the open file:

Code:
// to reset current position to beginning of file
   void rewind(FILE *f);   

   rewind(infile);
Or you can just close and reopen the file.
 
Old 12-16-2010, 01:56 AM   #3
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Original Poster
Rep: Reputation: 15
Great, thank you much!
 
Old 12-16-2010, 08:52 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Just a bit of possibly useful information...
If you open the file as a stream ( fopen() ), you have even more flexibility. You can get the current position within a file at any time with ftell(), and reset the current position to arbitrary values with fseek(). The arguments to fseek() allow you to specify a position relative to the beginning of the file, relative to the end of the file, or relative to the current position.

--- rod.

EDIT: Oops. I see you must already have opened the file with fopen().

Last edited by theNbomr; 12-16-2010 at 08:54 AM.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pseudo Terminal needs return before reading data MrUmunhum Programming 6 11-22-2008 02:10 PM
reading all physical volumes scan on the beginning matters Slackware 2 06-06-2007 12:19 PM
How do I write to the beginning of a file? abefroman Programming 1 06-30-2005 11:56 AM
How to delete a file beginning with '-'? zoomzoom Linux - General 2 02-02-2004 10:01 AM
read from file from the beginning? raven Programming 3 12-05-2003 08:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration