segmentation error

This is a discussion on segmentation error within the C Programming forums, part of the General Programming Boards category; hey guys.. am having problems reading a file into a linked list.. i am getting a segmentation error .. this ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    segmentation error

    hey guys..
    am having problems reading a file into a linked list.. i am getting a segmentation error .. this is my code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    struct listnode
    {
     int data1;
     int data2;
     int cost;
     struct listnode *nextPtr;
    };
    
    typedef struct listnode LISTNODE ;
    void print(LISTNODE);
    void read(LISTNODE *, FILE*);
    
    main()
    {
      FILE *fptr;
      char filename[255];
      LISTNODE *node;
    printf("enter filename\n");
      scanf ("%s",filename);
      fptr = fopen(filename,"r");
    
      if (fptr == NULL)
            {
          printf ("file cannot be found");
          exit(0);
            }
    
    read(node ,fptr);
    
    fclose(fptr);
    }
    
    
     void read(LISTNODE *node,FILE *fptr )
      {
        LISTNODE *Curr;
        Curr = node;
    
        do
          { fscanf(fptr, "%d %d %d", &(Curr->data1), &(Curr->data2), &(Curr->cost));
    printf("ssss\n");     // i used this on each line to test wer the error was..so im  thinking             i t                          has to do with fscanf 
    if (!feof (fptr))
            {
               Curr->nextPtr = malloc(sizeof(LISTNODE));
               Curr = Curr->nextPtr;
            }
    
     else
       {
         Curr->nextPtr = NULL;
       }
       }while (!feof(fptr));
       }
    i tried using dbg.. and i get a breakpoint at enter filename..what does that mean ?
    any help would very much be appreciated

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    void read(LISTNODE *node,FILE *fptr )
      {
        LISTNODE *Curr;
        Curr = node;
    
        do
          { fscanf(fptr, "%d %d %d", &(Curr->data1), &(Curr->data2), &(Curr->cost));
    It looks that you pass in an uninitialized pointer, but then use it to access the fields. Uninitialized pointers don't point to anything valid.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Code:
    void read(LISTNODE *node,FILE *fptr ) {    
        LISTNODE *Curr;
        Curr = node;
    if you have a NULL pointer in Curr, try to allocate memory by malloc(sizeof(LISTNODE)); for it, and then you will can to put a data to that memory

    I am using a chain like 3 - 2 - 1, when I add a node to the list, because it doesn't need to go for all nodes to get a tail and append

    something like this
    Code:
        ...
        Curr->next = head;
        head = Curr;
        return head;
    Last edited by c.user; 04-26-2009 at 06:05 AM.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    hey thanx guys .. its working now
    i allocated memory for Curr

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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