Solved

Using FGETS within a loop

Posted on 1998-08-04
3
533 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

739 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