Thread: Weird while loop problem

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    Weird while loop problem

    Hey all,

    So I have a file called h.list which is a text file containing all of the filenames of the working files the rest of my program will open sequentially and use. Now for some reason the loop always reads the last file in h.list twice though I see no reason why it should.

    Code:
     if((plist=fopen("h.list","r"))==NULL)
                    {
                            printf("Cannot open h.list \n");
                            exit(0);
                    }
    
                    while(!feof(plist)){
    
                            fscanf(plist,"%s", FILENAME);
                            printf("Read %s from h.list\n", FILENAME);
                            /* Open the file and read the points into arrays */
                            getpoints(FILENAME);
    
                            /* Perform the Radial Distribution Calculation */
                            nearest_neightbor(2);
                            /* Write n(r) and g(r) to a file  */
                            writeOutput(k);
                            k++;
                    }
    Any help would be greatly appreciated!

    Cheers,
    Paddon
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should read the FAQ that says "why you should never use feof to control a loop".

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    208
    Got it!

    Thanks
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weired problem in while loop
    By avisik in forum C Programming
    Replies: 14
    Last Post: 12-01-2008, 01:41 PM
  2. Problem with a loop
    By wiggsfly in forum C Programming
    Replies: 5
    Last Post: 11-30-2008, 12:45 PM
  3. while loop problem
    By tortan in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2006, 11:11 AM
  4. Problem with for loop
    By irsmart in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2006, 04:26 PM
  5. While loop problem
    By nadkingcole in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 06:14 AM