Thread: Program keeps crashing; attempting to extract each string (word) from text file

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    14

    Program keeps crashing; attempting to extract each string (word) from text file

    I am trying to write a program that will extract each string(word) from a text file and insert it into a linked list. The problem is my program keeps crashing and I cannot figure out why. I believe it is my insert method but I am not sure. Thanks!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    struct node
    {
      char *data;
      struct node *next;
    };
    
    
    void insertNode(struct node**, char *);
    void printList(struct node*);
    
    
    int main()
    {
      struct node *head = NULL;
      FILE *fptr;
      char file_name[20];
      char str[1000];
      int numOfChar;
    
    
      printf("Enter the name of the file: ");
      scanf("%s",file_name);
    
    
      printf("Enter the number of characters per line: ");
      scanf("%d",&numOfChar);
    
    
      fptr=fopen(file_name,"r");
    
    
      while(fgets(str, sizeof str, fptr) != NULL)
      {
        int nitems = fscanf(fptr, "%999s", str);
    
    
        while (nitems == 1)
        {
            insertNode(&head, str);
            nitems = fscanf(fptr, "%999s", str);
        }
    
    
      }
    
    
      fclose(fptr);
      printList(head);
    
    
      return 0;
    }
    
    
    void insertNode(struct node** nodeHead, char *data)
    {
        struct node* new_node = (struct node*) malloc(sizeof(struct node));
        struct node *last = *nodeHead;
        char *str;
    
    
        str= (char *)malloc(60*sizeof(char));
        strcpy(str, data);
    
    
        new_node->data  = str;
        new_node->next = NULL;
    
    
        if (*nodeHead == NULL)
        {
           *nodeHead = new_node;
           return;
        }
    
    
        while (last->next != NULL)
        {
            last = last->next;
        }
    
    
        last->next = new_node;
    }
    
    
    void printList(struct node* node)
    {
        while(node != NULL)
        {
            printf(" %s ", node->data);
            node = node->next;
        }
    }

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Please post exemplary input file which crashes - it will be easier to debug it.

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    14
    The input is an ASCll text file that contains the following information:

    The President shall be Commander in Chief of the Army and Navy of the United States, and of the Militia of the several States, when called into
    the actual Service of the United States; he may require the Opinion, in writing, of the principal
    Officer in each of the executive Departments, upon any subject relating to the Duties of their respective Offices,
    and he shall have Power to Grant Reprieves and Pardons for Offenses against The United States, except in Cases of Impeachment.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Unfortunately, I was unable to reproduce the problem. Line 37 has sizeof without parentheses, so it does not compile as it is. However, I fixed it (added parentheses) and the program runs fine displaying all text from the linked list (in Debug as well as in Release configuration).

    Considering error at line 37, are you sure the code posted above is the same you have currently on your machine?

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    14
    The code compiles on my machine without the parentheses on sizeof. I added the parentheses and it still crashes. I am using the Code::Blocks IDE which includes the GNU GCC compiler. I have no idea what is causing it to crash.

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by kmdv View Post
    Unfortunately, I was unable to reproduce the problem. Line 37 has sizeof without parentheses, so it does not compile as it is. However, I fixed it (added parentheses) and the program runs fine displaying all text from the linked list (in Debug as well as in Release configuration).

    Considering error at line 37, are you sure the code posted above is the same you have currently on your machine?
    sizeof is not a function call. It's an operator. It doesn't need parentheses. The parentheses that are sometimes necessary are actually the cast operator.

    Code:
    int x;
    size_t s1 = sizeof x;
    size_t s2 = sizeof (int);

  7. #7
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by algorism View Post
    sizeof is not a function call. It's an operator. It doesn't need parentheses. The parentheses that are sometimes necessary are actually the cast operator.

    Code:
    int x;
    size_t s1 = sizeof x;
    size_t s2 = sizeof (int);
    It does need parentheses if it is typename instead of expression. But here it is expression, so right, it is fine.

    Anyway, it does not crash for me and I could not find any error by looking at the code.

  8. #8
    Registered User
    Join Date
    Mar 2016
    Posts
    14
    ok thanks for your help

  9. #9
    Registered User
    Join Date
    Mar 2016
    Posts
    14
    Does anyone else have any ideas/suggestions/advice or a reason for why it is crashing?

  10. #10
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by blongnec View Post
    ok thanks for your help
    So you thank a guy that can't help you but don't bother thanking me for helping you in your other thread?
    F you, buddy!
    (I see what's wrong with your program, btw.)

  11. #11
    Registered User
    Join Date
    Mar 2016
    Posts
    14
    haha calm down. I forgot you even responded. I always appreciate any help that I can get.

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by blongnec View Post
    I am trying to write a program that will extract each string(word) from a text file and insert it into a linked list.
    Code:
       fptr=fopen(file_name,"r");
    
      while(fgets(str, sizeof str, fptr) != NULL)
      {
        int nitems = fscanf(fptr, "%999s", str);
    
    
        while (nitems == 1)
        {
            insertNode(&head, str);
            nitems = fscanf(fptr, "%999s", str);
        }
    You should examine this section to make sure it is what you intend. fgets reads a line from the file into str, but then inside your loop you are doing fscanf to read tokens (i.e. whitespace separated words) from the file handle. Maybe you meant to be using sscanf to read tokens from the line you just read? This is a case where you should think about what 'str' is. Choose a more descriptive name for it. Is it intended to hold a line of text, or a token?

    Also, write some pseudocode first to make sure it is what you intend:

    Code:
    while there are more lines
       read a line
       while there are more words in the line
          get a word
          insert the word into the list

  13. #13
    Registered User
    Join Date
    Mar 2016
    Posts
    14
    Will my program work correctly if i use sscanf? I have used a bunch of different functions to try and read in each word but my program still crashes. I am new to C so bear with me. Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching a specific string/word/etc in a text file?
    By zacharyrs in forum C Programming
    Replies: 5
    Last Post: 11-29-2009, 07:54 PM
  2. reading text-and-numbers file word by word
    By bored_guy in forum C Programming
    Replies: 22
    Last Post: 10-26-2009, 10:59 PM
  3. Extract Title from plain text file
    By Todd88 in forum C++ Programming
    Replies: 10
    Last Post: 11-21-2008, 09:47 AM
  4. How to extract (formatted) text from a PDF file?
    By HeinzB in forum C++ Programming
    Replies: 2
    Last Post: 08-19-2008, 02:46 AM
  5. Help reading text file word by word
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 05-25-2002, 05:13 PM