Solved

Using FGETS within a loop

Posted on 1998-08-04
3
522 Views
Last Modified: 2010-04-15
My program looks like this:

void main () {
    char buf [MAXLINE];
    double i;
    char * tmp = "in.file";
    FILE * infile = fopen (tmp, "r");

    for (i = 0.0; i <= 1.0; i += 0.01) {
        while (fgets(buf, MAXLINE, infile != NULL) {fprintf (stdout, "%f\n", i);}
    fclose (infile);
}

Now the problem I think is with the opening and closing b/c I can get it to go through the first iteration of the loop and that is all.  This is a very smaller size version of my code that just illustrtates the concept of looping around fgets.  If the char * tmp deal looks fishy, it is, but that is not how I have it in the code, and I know it is opening the file one single time, but then fgets returns null.
0
Comment
Question by:cerac000
3 Comments
 
LVL 2

Accepted Solution

by:
shchuka earned 50 total points
ID: 1251942
Your problem is that you're having two loops.  Before the outer loop, you're opening the file and on each iteration of the for() loop, you're reading the file until the end of file.  When you're getting to the second iteration of the for() loop, you're already at the end of the file and there's nothing to read.

There are two solutions to this problem.  One is to move fopen and fclose functions inside the for loop, thus getting something like this:

for(...) {
    fopen(...)
    while(...) {...}
    fclose(...)
}

The other solution is to use the rewind() function (is that the name?).  This way you'll have something like

fopen(...)
for(...) {
    while(...) {...}
    rewind(...)
}
fclose(...)

Either way should work just fine.

0
 
LVL 11

Expert Comment

by:alexo
ID: 1251943
Openning and closing are lenghty procedures.  Use rewind().
0
 

Author Comment

by:cerac000
ID: 1251944
Thank you all
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now