what could cause seg fault in this code?

This is a discussion on what could cause seg fault in this code? within the C Programming forums, part of the General Programming Boards category; I'm designing a program for appending a few lines at certain line from a file. the procedure is read from ...

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

    what could cause seg fault in this code?

    I'm designing a program for appending a few lines at certain line from a file.
    the procedure is read from a file & store the lines into a link list, append at certain line then write it to result.txt.
    However, my firstline is not working properly.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    
    
    typedef struct Node {
         char   line[100];
         struct Node *next;
    } NodeT;
    
    NodeT *makeNode(char *);
    int Append(char *,int);
    
    int main(int argc, char **argv) {
       NodeT *head;
       NodeT *cur;
       NodeT *new;
       NodeT *inputhead;
       NodeT *inputcur;
       NodeT *inputnew;
       NodeT *tmp;
       NodeT *tmp1;
       FILE *fp,*fpo;
       int linenum;
       char line[100];
       int i = 1;
    
       linenum = atoi(argv[1]);
       fp = fopen(argv[2],"r");
      
       while(fgets(line,100,fp) != NULL) {
          if(i == 1){      
             head = makeNode(line);
             head = cur;
             printf("%s",head->line);//shouldn't they print out the same things??
             printf("%s",line);
          }
          else{
             new = makeNode(line);
             cur->next = new;
             cur = cur->next;
          }
          
          i++;
       }
       i = 1;
       fclose(fp);
       while(1) {
          
          fgets(line,100,stdin);
          if(line[0] == '.') break;
          if(i == 1){
             inputhead = makeNode(line);
             inputcur = inputhead;
          }
          else{
             inputnew = makeNode(line);
             inputcur->next = inputnew;
             inputcur = inputnew;
          }
       }
       i = 1;
       cur = head; 
       while(i < linenum){
          tmp = cur;
          cur = cur->next;
          i++;
       }
       tmp->next = inputhead;
       inputcur->next = cur;
       tmp1 = head;
       fpo = fopen("result.txt","w+");
    
    
       while(tmp1 != NULL){
          fprintf(fpo,"%s",tmp1->line);
          tmp1 = tmp1->next;
       }
    
    
       return 0;
    }
    
    
    
    
    NodeT *makeNode(char *line) {
       NodeT *new;
       new = malloc(sizeof(NodeT));
       if (new == NULL) {
         
         exit(1); 
       }
       strcpy(new->line,line);
       new->next = NULL;
       return new;
    }

  2. #2
    Registered User
    Join Date
    May 2011
    Posts
    22
    sry is not causing seg fault any more... just print some random stuff in the first line of my result.txt

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    head = makeNode(line);  /* makes a new node and sets it at the head */
             head = cur; /* completely destroys the linked list */

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    22
    omg....thx... stupid mistake...lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fault with code but unsure where!
    By aadil7 in forum C Programming
    Replies: 6
    Last Post: 12-09-2010, 12:07 PM
  2. Help with Segmentation fault - in simple code
    By ramchan in forum C Programming
    Replies: 8
    Last Post: 03-01-2009, 08:07 AM
  3. Segmentation Fault before faulty code is ever reached?
    By yougene in forum C Programming
    Replies: 21
    Last Post: 01-12-2009, 05:44 PM
  4. Help with code segmentation fault
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 04-10-2003, 03:00 PM

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