[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using FGETS within a loop

Posted on 1998-08-04
3
Medium Priority
?
541 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

873 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