Thread: C programming Seg fault help

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    22

    C programming Seg fault help

    This is my program to read a file & print the lines out,
    since im not familiar with link list, i got a seg fault from this.
    Anyone can help me out,thx?


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    typedef struct Node {
         char   line[1000];
         struct Node *next;
    } NodeT;
    
    int main(int argc, char **argv) {
       
    
       char line [1000];
       FILE *fp;
       NodeT *s, *head;
       head = s;
    
       if(argv[1] != NULL){   
          if((fp = fopen(argv[1],"r")) != NULL) {
             while (fgets(s->line, 1000, fp) != NULL) {
             printf("%s" ,line);
             s = s->next;
             }
             s->next = NULL;
          }
       }
       free(s);
       free(head);
    
       head = NULL;
       s = NULL;
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    You need to malloc() memory for `s' - at the moment, `s' and `head' both point to some unknown location.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    22
    i did put them but forgot to post them ;P i free them at the end....but after i malloc those two, im still getting seg fault.

    NodeT *s = malloc(sizeof(NodeT* ));
    NodeT *head = malloc(sizeof(NodeT* ));

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    22
    This is what i got by now
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    typedef struct Node {
         char   line[1000];
         struct Node *next;
    } NodeT;
    
    int main(int argc, char **argv) {
       
    	
       char line [1000];
       FILE *fp;
       NodeT *s = malloc(sizeof(NodeT* ));
       NodeT *head = malloc(sizeof(NodeT* ));
    
       if(argv[1] != NULL){   
          if((fp = fopen(argv[1],"r")) != NULL) {
             while (fgets(s->line, 1000, fp) != NULL) {
             printf("%s" ,line);
             s = s->next;
             }
             s->next = NULL;
          }
       }
       free(s);
       free(head);
    
       head = NULL;
       s = NULL;
    
    return 0;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Linked list - Wikipedia, the free encyclopedia
    You need a malloc for each line you read.

    Code:
    char buff[BUFSIZ];
    while ( buff, BUFSIZ, fp ) != NULL ) {
      // malloc a new node
      // copy buff
      // append node to the list
    }
    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.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    22
    Code:
       if(argv[1] != NULL){   
          if((fp = fopen(argv[1],"r")) != NULL) {
             while (fgets(line, 1000, fp) != NULL) {
    				s = malloc(sizeof(NodeT*));
    				strcpy(s->line,line);
    				s = s->next;
             }
             s->next = NULL;
          }
       }
    i guess assume that's what you want me to do.
    but still seg fault ;(
    am i doing something wrong in other line?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Look at the wiki page.
    s = s->next is not going to append anything to the end of an existing list.
    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.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    22
    I'm not quite getting it, say s is a pointer that will go through every nodes in the list, should I do "s = s -> next" to move pointer s to the next node?

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    22
    oh wait..the problem occur after the loop, thx a lot for ur help!

  10. #10
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by vince3214 View Post
    I'm not quite getting it, say s is a pointer that will go through every nodes in the list, should I do "s = s -> next" to move pointer s to the next node?
    Code:
    				s = malloc(sizeof(NodeT*));
    				strcpy(s->line,line);
    But after those two lines, what is the value of s->next? Hint: You have not set it yet, so it is undefined or indeterminate.

  11. #11
    Registered User
    Join Date
    May 2011
    Posts
    22
    should I initialize ever s->next = NULL then give them value in the loop?

  12. #12
    Registered User
    Join Date
    May 2011
    Posts
    22
    *every

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why did you make a new post instead of hitting the big edit button?


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to programming, Segmentation Fault?
    By dc210 in forum C Programming
    Replies: 3
    Last Post: 09-14-2010, 12:05 AM
  2. Replies: 4
    Last Post: 04-20-2010, 10:55 PM
  3. Replies: 5
    Last Post: 10-14-2009, 05:47 AM
  4. C Programming question (segmentation fault)
    By trustno1fox in forum C Programming
    Replies: 2
    Last Post: 05-01-2009, 04:55 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM