need help, fgets won't stop while loop

This is a discussion on need help, fgets won't stop while loop within the C Programming forums, part of the General Programming Boards category; I'm using the following code in order to order to split up a file into a linked list filled with ...

  1. #1
    Registered User
    Join Date
    May 2006
    Location
    WV
    Posts
    11

    need help, fgets won't stop while loop

    I'm using the following code in order to order to split up a file into a linked list filled with words and several paragraphs. My question is that it seems that my while loop doesn't seem to be stopping at the end of the file. Is it possible that the file I am using does not an EOF tag or something?

    Code:
    while(fgets(currentLine, 125, fptr) != 0){
    
          if(currentLine[0] == '\0'){
             currentPara->nextParagraph = malloc(sizeof(PARAGRAPHPTR));
             currentPara = currentPara->nextParagraph;
             currentPara->start = malloc(sizeof(LISTNODEPTR));
             currentWord = currentPara->start;
             currentWord->nextPtr = NULL;
             currentWord->length = 0;
              printf("After?");
              printf("\n");
          }
          else{ for( i =0; i <= 124 && testChar != 0; i++){
           printf("%d %d ", currentWord->length, i);
    
             printf("\n");
             testChar = currentLine[i];
             printf("\n");
             if(testChar >= 33 && testChar < 127){
                 printf("%c ", testChar);
                currentWord->data[currentWord->length] = testChar;
                currentWord->length++;
           
             }
             else{
                currentWord->nextPtr = malloc(sizeof(LISTNODEPTR));
                currentWord = currentWord->nextPtr;
                currentWord->nextPtr = NULL;
                currentWord->length = 0;
             }
    
          }
          }
       }
    Thanks for your help.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Keep calling feof to determine if you've reached the end of the file. feof returns true after the first read operations that tries to read beyond the end of the file.
    It's usually better to do an infinite loop or something and check for end-of-file with feof and break when feof(...) == true.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    while(fgets(currentLine, 125, fptr) != NULL){
    And dont use feof read FAQ

    ssharish

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    feof is fine if you use it after a read. Just don't use it as a loop condition.
    fgets can fail even if it didn't read past the end of the file (read error from disk, maybe, CRC error).

  5. #5
    Registered User
    Join Date
    May 2006
    Location
    WV
    Posts
    11
    I changed the 0 to NULL, and added the following:
    Code:
    if(feof(fPtr))
       break;
    at the beginning of the loop and it still gets stuck in an infinite loop.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    If you remove the body of the while loop, does it then exit as expected?

    > currentWord->data[currentWord->length] = testChar;
    1. How is the data member allocated?
    2. How do you stop buffer overflow?

    From the information provided, there is no reason why repeated calls to fgets() would not return NULL at some point when reading a file.
    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.

  7. #7
    Registered User
    Join Date
    May 2006
    Location
    WV
    Posts
    11
    Could the data member being poorly allocated or buffer overflow cause this to get caught in an infinite loop? Here's the declaration and the allocation:
    Code:
    struct listNode {  /* self-referential structure */
       char data[30];
       int length;/*Length of the word, will actually be low by one*/
       struct listNode *nextPtr;
    };
    typedef struct listNode LISTNODE;
    typedef LISTNODE *LISTNODEPTR;
    
    struct paragraph { /* The starting nodes of each paragraph, as well as links to the next paragraph*/
       LISTNODEPTR start;
       struct paragraph *nextParagraph; 
    };
    
    typedef struct paragraph *PARAGRAPHPTR;
    
    
    void insert(LISTNODEPTR *);
    void pop(LISTNODEPTR *, char[]);
    int isEmpty(LISTNODEPTR);
    
    main()
    {
       /* Declarations */
    
       char currentLine[125];
       char tempWord[30];
       int wordsInLine, extraSpaces;
       int linePos, lineSize, wordSize=0;
       int i, j;
       char filename[20];   
       FILE *fptr;
       int testChar = '0';
       int newParagraph = FALSE;
       int newWord = FALSE;
       LISTNODEPTR endOfLine;
       LISTNODEPTR startOfFile;
       endOfLine = malloc(sizeof(LISTNODEPTR));
       startOfFile = malloc(sizeof(LISTNODEPTR));
    
       startOfFile->nextPtr = NULL;
    
    
       startOfFile->length = 0;
       PARAGRAPHPTR firstPara;
       firstPara = malloc(sizeof(PARAGRAPHPTR));
       firstPara->start = startOfFile;
       firstPara->nextParagraph = NULL;
       PARAGRAPHPTR currentPara = firstPara;
       LISTNODEPTR currentWord = startOfFile;
       currentWord->length = 0;
       printf("&#37;d ", currentWord->length);
    
    
    
     /* Get the file and user input*/
       printf("Please input the file that you would like to get text from\n");
       scanf("%s", filename);
       fptr = fopen(filename, "r");
        
       printf("Please input the number of characters per line for the text\n");
       scanf("%d", &lineSize);
       currentWord->length = 0;
       /*Put the file into paragraphs*/
    and then the above code is used. I know that it's an infinite loop, it's not exiting early or anything. Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  3. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  4. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM
  5. 2 largest elements; -1 to stop loop
    By Peachy in forum C Programming
    Replies: 4
    Last Post: 09-16-2001, 05:16 AM

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