Infinite loop

This is a discussion on Infinite loop within the C Programming forums, part of the General Programming Boards category; I'm trying to read lines of text from the file pointed to by `fp'; the inner while continues forever for ...

  1. #1
    Caution: Wet Floor
    Join Date
    May 2006
    Posts
    55

    Infinite loop

    I'm trying to read lines of text from the file pointed to by `fp'; the inner while continues forever for some reason. Doesn't fgets append the string terminator to `thisline' after reading a line from the file? Thanks.

    Code:
    #define MAX_COLUMNS 80
    
    /* ... */
    
    while(NULL != fgets(thisline, MAX_COLUMNS + 1, fp)){
    
             i = 0;  // Reset counter for next line
    
             /* ... */
    
             while('\0' != thisline[i]){
    
             /* Can't get out of this! */
                    i++;
               }
    }

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Think about each line in the file, what does each line end with? Compare that value to what the actual file ends with. Are they different?


    [EDIT]

    Hmm, after thinking about this for a while, it does seem strange that the inner loop is infinite.
    Last edited by bivhitscar; 05-27-2006 at 08:03 PM.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    Caution: Wet Floor
    Join Date
    May 2006
    Posts
    55
    Think about each line in the file, what does each line end with? Compare that value to what the actual file ends with. Are they different?
    Doesn't each line end with an '\n'? I tried doing one of these:

    Code:
    while(('\n' != thisline[i]) || ('\0' != thisline[i])){
    
           /* Inner loop. */
           i++;
    }
    but the result is the same. Do I need to compare against EOF too?

  4. #4
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Yeh, my initial thoughts about the newline character were wrong. I'm just testing the code now to see what the issue is.

    [EDIT]
    You might need to post the rest of your code, because this works fine for me:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX_COLUMNS 80
    
    int main(int argc, char *argv[])
    {
        int i;
        FILE *fp;
        char *filename = "balls.txt";
        char thisline[100];
        
        fp = fopen( filename, "r" );
        
        while ( NULL != fgets(thisline, MAX_COLUMNS + 1, fp))
        {
              i = 0;
              
              while ( '\0' != thisline[i] )
              {
                    i++;
                    printf("%d\n", i);
              }
        }
    
        system("PAUSE");	
        return 0;
    }
    Text file:

    1asd asdas as asf asf
    2ihasdia sdqad qewf qw ef
    3khafdwe wef we fwe f
    Last edited by bivhitscar; 05-27-2006 at 08:17 PM.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  5. #5
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    [EDIT v2]

    Bahahaha, I'm an idiot, I was right.

    This line should be ( for future reference ):

    Code:
    while(('\n' != thisline[i]) && ('\0' != thisline[i]))
    Last edited by bivhitscar; 05-27-2006 at 08:38 PM.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  6. #6
    Caution: Wet Floor
    Join Date
    May 2006
    Posts
    55
    OUCH! Assignment issue. Usually I'm good about keeping the lvalues to the left! Variable `i' is scoped globally, too.


    Code:
    while('\0' != thisline[i]){
    
        switch(thisline[i]){
    
                  case foo: 
                       if(i = 0){ do something;}
                          break;
    
              /*...*/
                }
           i++;
    }

  7. #7
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Yep, that'll do it. :P
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    > OUCH! Assignment issue. Usually I'm good about keeping the lvalues to the left!
    You're focussing on the wrong issue.
    which one is better?
    You need to increase the warning level of your compiler, or get a compiler which will warn you about using = where == was more likely.

    > if(i = 0)
    Of course, if this had been in your first post rather than your third, then the answer would have been much more obvious.
    Don't paraphrase code by snipping bits out, because you're just going to snip out the bit which is important.

    > fgets(thisline, MAX_COLUMNS + 1, fp)
    Don't lie about the size of your buffer to fgets(). If you want to add 1, add it in the array
    char thisline[MAX_COLUMNS+1];
    fgets( thisline, sizeof thisline, fp );

    > char thisline[100];
    This has nothing to do with your macro size - change either and fgets() could be in big trouble.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Quote Originally Posted by Salem
    > char thisline[100];
    This has nothing to do with your macro size - change either and fgets() could be in big trouble.
    Yeh sorry, that was my mistake, I just did it for my testing.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-15-2009, 12:24 AM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 01:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 01:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 12:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21