Solved

Using FGETS within a loop

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where is my core dump file in Ubuntu? 12 454
valid enum? 6 75
change colour of repeater control in asp.net c# 7 114
C++ :Change value from  DisableCMD registry 4 66
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

828 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