Thread: Reading A Text File Into A Linked List

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    3

    Question Reading A Text File Into A Linked List

    I am trying to read in a text file and add strings from it word by word into a Linked List. I'm fairly new at C and don't quite understand pointers. I've had a few different errors just messing around with it, but now I'm getting an infinite loop within my main program.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    struct listNode {  /* self-referential structure */
       char data[50];
       struct listNode *nextPtr;
    };
    
    typedef struct listNode LISTNODE;
    typedef LISTNODE *LISTNODEPTR;
    
    void insert(LISTNODEPTR *, char[]);
    char delete(LISTNODEPTR *, char[]);
    void printList(LISTNODEPTR);
    int fpeek(FILE *);
    
    main() {
    
        FILE *fptr;
        char file_name[20];
        int nrchar = 0;
        LISTNODEPTR startPtr = (struct listNode *) malloc(sizeof(struct listNode));
        startPtr->nextPtr = NULL;
        char word[50];
        char c;
        int i;
    
        printf("What is the name of the file in which the text is stored?\n");
        scanf("%s",file_name);
        //    printf("Type the number of characters per line");
        //scanf("%d", &nrchar);
        fptr = fopen(file_name,"r");
              while(fpeek(fptr) != EOF) {
          i = 0;
          while(fpeek(fptr) != ' '){
            word[i] = fgetc(fptr);
            i++;
            printf("%d",i);
          }
          word[i] = '\0';
          insert(&startPtr, word);
          printf("%c", word[4]);
          
           }
        fclose(fptr);
        printList(startPtr);
        
    
    return 0;
    }
    
        /* Insert a new value into the list in sorted order */
        void insert(LISTNODEPTR *sPtr, char value[])
        {
          LISTNODEPTR newPtr, currentPtr;
    
          newPtr = malloc(sizeof(LISTNODE));
          strcpy(newPtr->data, value);
          newPtr->nextPtr = NULL;
          currentPtr = *sPtr;
    
          while(currentPtr && currentPtr->nextPtr){
            currentPtr = currentPtr->nextPtr;
          }
          currentPtr->nextPtr = newPtr;
           
        }
    
    
        /* Return 1 if the list is empty, 0 otherwise */
        int isEmpty(LISTNODEPTR sPtr)
        {
           return sPtr == NULL;
        }
    
        /* Print the list */
        void printList(LISTNODEPTR currentPtr)
        {
           if (currentPtr == NULL)
              printf("List is empty.\n\n");
           else {
              printf("The list is:\n");
    
              while (currentPtr != NULL) {
                 printf("%s --> ", currentPtr->data);
                 currentPtr = currentPtr->nextPtr;
              }
    
              printf("EOF\n\n");
           }
        }
    
        int fpeek(FILE *stream) {
            int c;
            c = fgetc(stream);
            ungetc(c, stream);
            return c;
        }
    When I run my full code, it prints 12345ooooooooooooooooooooooo...etc. In my test file the first word is "Hello" so that's where the infinite 'o's come from. If the outer loop is the one that is infinite, then wouldn't the second while loop also execute more than once? What I mean is, why is the second print statement the only one that repeats itself?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So what happens when fpeek(fptr) does in fact equal a space? The outer condition is still true (space is not EOF), the inner condition will always be false (since you don't have a way to get past the space), so the outer loop spins endlessly while the inner loop can never start. You need to clear that space (easiest way is to read it in out of the file) so that you can move on to the next word.

    (The real fun will start if you don't have a space after your last word....)

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    Why doesn't the fpeek function check the next character after the space? Is there a better function to use for reading in characters from a file so that I wouldn't have to worry about clearing the space?

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Its getting put back into the stream when you call ungetc, ultimately keeping the character there I believe.

    Edit: you could say something like this to move along the character.
    Code:
    while(fpeek(fptr) != ' '){
            word[i] = fgetc(fptr);
            i++;
            printf("%d",i);
          }
    if(fpeek(fptr))==' ')
    char garbage = fgetc(fptr);
    Last edited by camel-man; 10-15-2013 at 07:18 PM.

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    Thank you, using the garbage variable along with adding another check for EOF in the second loop made it work.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I do not understand need in fpeek at all - as if you are trying not to read some characters from file?.. You need to read all and store only some parts...
    So read all and then decide what to do

    Code:
    int ch;
    while((ch = fgetc(f)) != EOF)
    {
       if(isspace(ch))
       {
         /* word ended - store it, if it contains something */
       }
       else
      {
          word[i] = ch; 
          i++;
       }
    }
    /* here store the last word if file was not properly ended with \n */
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list and file reading.
    By mgracecar in forum C Programming
    Replies: 1
    Last Post: 03-22-2012, 07:11 PM
  2. reading in file to linked list?
    By rickyson49 in forum C Programming
    Replies: 2
    Last Post: 03-31-2011, 09:07 AM
  3. Reading Text file in Linked list!
    By satty in forum C Programming
    Replies: 20
    Last Post: 07-29-2010, 08:48 AM
  4. Reading from a file into a linked list.
    By Wiretron in forum C Programming
    Replies: 5
    Last Post: 01-23-2006, 08:24 AM
  5. Reading a file into a Linked List
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-20-2002, 07:08 AM

Tags for this Thread