Thread: adding text from file to a linked list

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    7

    adding text from file to a linked list

    i am trying to add words from a text file to a linked list. my code works fine , but if the text file is too big it shows the following error: stack smashing detected. Can anyone help me please here is my code
    Code:
    #include <stdlib.h> 
    #include <string.h> 
    #include <time.h> 
    #include <stdio.h>
    FILE *f;
    
    
    int wordc=-1;
    struct list {
      char  *str;
      struct list *next;
     
    };
    struct list *root;
    struct list *head;
    struct list *curr=NULL;
    int init_list(){
      root=(struct list *)malloc(sizeof(struct list));
       head=root;
      if(root==NULL ){
        printf("Init list failed\n");
        return(1);
      }
        root[0].next=NULL;
        return(0);
    }
    int  list_add(char string[],int wordc){
        struct list *curr=(struct list *)malloc(sizeof(struct list));
        char *str1;
          if(root==NULL ){
                printf("Add list failed\n");
                return(1);
          }
        str1= (char *)malloc(60*sizeof(char));
          strcpy(str1,string);
        curr->str=str1;
        if(wordc==0)
            head->next=curr;
        else
            root->next=curr;
        
        curr->next=NULL;
        root=curr;    
        
    }
    void print_list(){
        struct list *ptr;
        ptr=head;
        while(ptr!=NULL){
            printf("|%s|\n", ptr->str);
            ptr=ptr->next;
        }
        printf("\n");
    }
    int open_file(char fname[]){
         FILE *f;
         char ch;
         char buf[60];
         int i=0;
         f=fopen(fname,"r");
             if ( f== NULL ){ 
            printf("ERROR OPENING THE FILE\n");
             return(1);
           }   
        //word count
         do
          {
            if((ch=fgetc(f))==EOF)
                break;
            do{
                
                buf[i]=ch;
                i++;
            }while((ch=fgetc(f))!='\n');
            buf[i]='\0';
            wordc++;
            printf("1\n");
            list_add(buf,wordc++);        
            i=0;
            printf("2\n");
                             
        
      }while(1);
    }
    int main(int argc,char *argv[]){
    
    
     if(argc!=2){
        printf("wrong parameters\n");
        return(1);
     }
     init_list();
     open_file(argv[1]);
     print_list();
    
    
    }

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    You haven't Google for the problem you are facing. Just do it and you will be able to find reason and solution, it is easy.
    S_ccess is waiting for u. Go Ahead, put u there.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    This appears to be a mistake on my part; In function init_list() what should be the value of wordc when the function is done?
    Why does the value of wordc matter in list_add function.

    NOTE: The use of root to mean tail is not good naming.

    The over use of global variables is one that you should reduce in the future.

    Edit: Turn ON compiler warning many of your functions are missing return statements!
    Code:
    ||=== testc, Debug ===|
    C:\Users\tsta8844\cb\testc\main.c||In function 'main':|
    C:\Users\tsta8844\cb\testc\main.c|97|warning: control reaches end of non-void function [-Wreturn-type]|
    C:\Users\tsta8844\cb\testc\main.c||In function 'open_file':|
    C:\Users\tsta8844\cb\testc\main.c|84|warning: control reaches end of non-void function [-Wreturn-type]|
    C:\Users\tsta8844\cb\testc\main.c||In function 'list_add':|
    C:\Users\tsta8844\cb\testc\main.c|45|warning: control reaches end of non-void function [-Wreturn-type]|
    ||=== Build finished: 0 errors, 3 warnings ===|
    Edit: In openfile, the variable ch must be int because EOF does NOT fit in char on many systems.
    Edit: Your loop for reading from the file has flaws; (I would guess) if the test files does not end with newline the loop will never end.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    Tim S.
    Last edited by stahta01; 06-05-2012 at 11:47 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading Text file in Linked list!
    By satty in forum C Programming
    Replies: 20
    Last Post: 07-29-2010, 08:48 AM
  2. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:13 PM
  3. print a text-file in linked list
    By tidemann in forum C Programming
    Replies: 10
    Last Post: 09-19-2006, 05:22 AM
  4. Why won't this work? (Linked List and text file)
    By omishompi in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2005, 12:24 PM
  5. Adding to linked list from external file
    By cghv in forum C Programming
    Replies: 1
    Last Post: 03-09-2005, 02:05 PM

Tags for this Thread