LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 02-02-2004, 09:25 PM   #1
Claus
Member
 
Registered: Jul 2003
Location: Santiago de Chile
Distribution: Debian testing/unstable
Posts: 74

Rep: Reputation: 15
C fscanf / feof question.


Hi

I always have the same doubt, and I always forget to ask....

With this piece of code:

Code:
 if (!(f = fopen (filename, "r")))
                return;
                                                                                                                             
        while (!feof (f)) {
                fscanf (f, "%s", data);
                printf("%s\n", data);
        }
I always get the last line of the file printed twice... Why is that happening???

What is the correct way to loop in a file?

Thank you.
 
Old 02-02-2004, 11:55 PM   #2
Berng
Member
 
Registered: Dec 2003
Location: Siberia,Russia
Distribution: Basically RedHat
Posts: 34

Rep: Reputation: 15
Basically,
you could try change the code
from
fscanf (f, "%s", data);
printf("%s\n", data);
to
if(fscanf (f, "%s", data)==1)
printf("%s\n", data);
to be sure that you succesfully read your variable from file before print it
Best Regards,
Oleg.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
fscanf returns -1 trutnev Programming 2 06-23-2005 08:50 AM
function who remplace feof linuxanswer Programming 1 04-22-2004 06:19 PM
fscanf & fgetc help billybob2004 Programming 2 02-04-2004 10:24 AM
fscanf c programming tomato Programming 14 01-09-2004 08:33 PM
using fscanf function Linh Programming 2 07-14-2003 12:58 PM

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

All times are GMT -5. The time now is 11:44 PM.

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