Thread: open a binary file and save/edit a linked list and load it

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    20

    help me about binary files

    this is my structure
    Code:
    typedef struct pokemon{
              char name[20];
              char type[20];
              struct pokemon * next;
    }pokemon_t;
    lets assume that the file has already data in it.

    i know that if i will load the file, i must link the nodes again, but i made the "linking of nodes again" as a function,

    because what i did in my function was, i open the file (fopen=("file.bin", "ab+", read (i use fread) the structures, link it, and return the head pointer (so i can access the list if i want to add or delete nodes)

    1. will i close the file? if i close the file, does my program still have the linked lists? if i will not close it, then when will i close it?

    then another function for adding of nodes, then after adding the node i will write it all again in the file. So it means, I will write all the nodes again (as in from the first node), so i will use fopen=("file.bin", "wb+")

    1. is it ok to do that?

    is there a problem with it? can you explain to me step by step what should i do in adding or deleting nodes.

    thank you. all of your comments will be a great help to me.
    Last edited by eyescythe; 10-17-2009 at 03:04 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you write the pointer, whatever value ends up in that spot in the file, is garbage when you read it in. So, basically you want this:
    Code:
    while (read one structure from file) != EOF/error 
        pass structure to a function which links it to your in-memory linked list, or just do it manually
    Basically you want to ignore the pointer's value when you read it back in, and just assign over top of that value. See any random linked list tutorial for adding or deleting nodes.


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

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    20
    Quote Originally Posted by quzah View Post
    If you write the pointer, whatever value ends up in that spot in the file, is garbage when you read it in. So, basically you want this:
    Code:
    while (read one structure from file) != EOF/error 
        pass structure to a function which links it to your in-memory linked list, or just do it manually
    Basically you want to ignore the pointer's value when you read it back in, and just assign over top of that value. See any random linked list tutorial for adding or deleting nodes.


    Quzah.
    but i have problems on writing the linked list on the binary file...
    Code:
             assuming that head is the head pointer of the linked list..
    
    	current=head;
    	while(current!=NULL){
    		fwrite(&pokemon, sizeof(pokemon_t), 1, bfp);
    		current=current->next;
    	}
    is this correct?

    and i read it using this code...
    Code:
    	i=0;
    	while(!feof(bfp)){
    	fseek(bfp, sizeof(pokemon_t)*i, SEEK_SET);
    	fread(&pokemon, sizeof (pokemon_t), 1, bfp);
    	printf("Name is %s\n", pokemon.name);
    	printf("Type is %s\n", pokemon.type);
    	i++;
    	}
    is the code in writing the linked list into the file wrong?
    or is it the code in reading??

    pls explain to me

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Again, (1) if current is walking your list, then current is what you need to be writing to the file, and (2) since write takes a pointer to the data, and current is a pointer to the data, no decoration of current is necessary.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    20
    Quote Originally Posted by tabstop View Post
    Again, (1) if current is walking your list, then current is what you need to be writing to the file, and (2) since write takes a pointer to the data, and current is a pointer to the data, no decoration of current is necessary.
    so is this correct?

    Code:
          current=head;
    	while(current!=NULL){
    		fwrite(current, sizeof(pokemon_t), 1, bfp);
    		current=current->next;
    	}

Popular pages Recent additions subscribe to a feed