Problem extracting data from a large file and passing it to a linked list

This is a discussion on Problem extracting data from a large file and passing it to a linked list within the C Programming forums, part of the General Programming Boards category; Every time I try to extract data from a file that I have, it extracts the 2 first numbers and ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    16

    Problem extracting data from a large file and passing it to a linked list

    Every time I try to extract data from a file that I have, it extracts the 2 first numbers and then it throws me a Segmentation fault. Here's the code of the two functions that are used:

    Code:
    void extraer()
    {
    	primer = ult = NULL;          //primer = first node ; ult = last node;
    	int numero;
    
    	FILE *fichero;
    	fichero=fopen("datos.txt", "r");
    
    	if (fichero==NULL)
    	{
    		printf("\nEl fichero no existe\n");
    		menu();
    	}
    
    	while(!feof(fichero))
    	{
    		struct lista *neue;
    		neue = new lista;
    		fscanf(fichero, "%d", &numero);
    		neue->id=numero;
    		metelist(neue);                    //Inserts the node with the value into the list
    	}
    	fclose(fichero);
    }
    
    void metelist(struct lista *neue)              //Pushes the node into the linked list
    {
         if(primer==NULL)
         {
              primer = neue;
              ult = neue;
         }
         else
         {
              primer->next = neue;
              ult = ult->next;
         }
         neue->next = NULL;
         ult->next = NULL;
    }
    When I try to run it, it extracts the first 2 numbers and then I get a Segmentation fault, and gdb says this:
    Code:
    Program received signal SIGSEGV, Segmentation fault.
    0x0000000000400fae in metelist(lista*) ()
    I'm sorry if it's a noobish question.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Question 12.2

    neue = new lista;

    is it C or C++? Choose only ONE. and post to the correct forum next time.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void metelist(struct lista *neue)              //Pushes the node into the linked list
    {
         if(primer==NULL)
         {
              primer = neue;
              ult = neue;
         }
         else
         {
              primer->next = neue;
              ult = ult->next;
         }
         neue->next = NULL;
         ult->next = NULL;
    }
    If I look at this, I assume that primer is the head of your list. This code either puts the new node as the head, if there isn't one, or it puts this node as the primer->next node, completely ignoring if there is already a primer->next. This I think should instead just be:
    Code:
    void foo( struct node *n )
    {
        n->next = primer;
        primer = n->next;
    }
    Or something? I'm not sure what you are really thinking you're doing here, but I'm pretty sure you're doing it wrong.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing an array to linked list
    By bar338 in forum C Programming
    Replies: 7
    Last Post: 04-08-2009, 10:15 PM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 03:29 PM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  5. passing data
    By vnrabbit in forum C Programming
    Replies: 4
    Last Post: 11-20-2002, 03:19 PM

Tags for this Thread


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