Fget to list

This is a discussion on Fget to list within the C Programming forums, part of the General Programming Boards category; Hi, i've a problem with fget, cause when i try to open the file i get Segmentation Fault...Anyone knows whats ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    34

    Fget to list

    Hi, i've a problem with fget, cause when i try to open the file i get Segmentation Fault...Anyone knows whats wrong?


    Code:
    void fgetrestaurante(List Mainlist) {
    	
    	List random=cria_lista(random);
    	List Last=cria_lista(Last);
    	Last=(List)malloc(sizeof(List_node));
        int cnt=-1;
        Restaurante rest;
        random=Mainlist->next;
        
        
        FILE *fp = fopen("data.dat", "rb");
    	if (fp != NULL){
    		while(!feof(fp)){
    			cnt=fread(&rest, sizeof(Restaurante), 1, fp);
    			if (cnt){
    				random->info=rest;
    				random->next=(List) malloc (sizeof (List_node));
    				Last=random;
    				random=random->next;
    			}else
    				Last->next=NULL;
    		}           
    	}
    	fclose(fp);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    random == NULL, perhaps; or perhaps Last does when you get there. Maybe print out values of Last and random before you use them, and see what happens.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    Your code isn't calling fgets().

    Also, read the FAQ on how not to use feof()
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Are you sure you get Segmentation Fault when opening the file? Try debugging if you are not sure. Thus putting a printf("OK"\n) after some parts of the code to see where it crashes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 04:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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