Solved

Using FGETS within a loop

Posted on 1998-08-04
3
528 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

831 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