1,105,310 Community Members

how do you loop fscanf until EOF in c?

Member Avatar
potato_bum
Newbie Poster
4 posts since Mar 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

for example.. number.txt: (has these contents)
1.75:2.00:3.00
2.00:5.00:7.24
3.00:6.35:1.00
--
my source code is..
#include <stdio.h>
int main()
{
FILE *infile;
double a, b, c;
char d;
double sum;


infile = fopen("number.txt", "r");
if(infile == NULL)
{
printf("number DOES NOT EXISTS!");
}
else
{
while(!foef(infile)) /*here's my problem*/
{
fscanf(infile, "%lf%c%lf%c%lf\n", &a,&d,&b,&d,&c); /*fscanf is used*/
printf("%f %f %f", a, b, c); /*just for checking*/
sum = a + b + c;
printf("sum is %f", sum);
}

}

fclose(infile);
return 0;
}
--
problem is..
if i put the while loop, it won't compile. it has these error message saying "undefined reference to `foef'".
if i remove the while loop, the fscanf reads only the first line.
i would like to read also the other data from line 2 and line 3 and solve their sum respectively.
so how can i loop the the fscanf?(assuming that i don't know how many lines are in the file? or maybe its safe to say to loop the fscanf until EOF?)
thanks a lot!

Member Avatar
Aia
Nearly a Posting Maven
2,307 posts since Dec 2006
Reputation Points: 1,977 [?]
Q&As Helped to Solve: 236 [?]
Skill Endorsements: 20 [?]
 
0
 

Oops! you posted the same question twice.

Member Avatar
thekashyap
Practically a Posting Shark
809 posts since Feb 2007
Reputation Points: 193 [?]
Q&As Helped to Solve: 77 [?]
Skill Endorsements: 0 [?]
 
0
 

STFW
Here is what I found for you:

while (fscanf(in_fd, "%s", username) != EOF) {
  fprintf(out_fd, "%s\n", username);
}

PS: Remember fscanf returns EOF in case of errors or if it reaches eof. So you can check return value of fscanf instead of feof().

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article