Thread: SAVE data from double linked list into .bin file and load it...

  1. #1
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194

    SAVE data from double linked list into .bin file and load it...

    hi...

    i'm trying to save data from a double linked list into a .bin file and then load the data from the same file to the dynamic memory whenever i want.

    Code:
    typedef struct marcarConsul noCons, *pnoCons;
    struct marcarConsul
    {
    	int idPaciente;
    	int idMedico;
    	int especialidade;
    	int horaConsulta;
    	int faixaEtaria;
    	int duracao;
    
    	int tipoConsulta;
    	char desc[100];
    
    	Data dataConsul;
    	Hora horaMarcacao;
    
    	pnoCons prox;
    	pnoCons prev;
    };
    how do i do that?
    "Artificial Intelligence usually beats natural stupidity."

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you traverse the list from the beginning to the end (define these 2 points by yourself) and write each node to the disk

    when reading - again you read node by node - one at a time, and restore the pointers as if you are inserting new node to the list (because values stored to the disk will have no meaning)

    to make it simpleir task you may want slightly restructure your data like

    Code:
    struct data
    {
    	int idPaciente;
    	int idMedico;
    	int especialidade;
    	int horaConsulta;
    	int faixaEtaria;
    	int duracao;
    
    	int tipoConsulta;
    	char desc[100];
    
    	Data dataConsul;
    	Hora horaMarcacao;
    };
    struct node
    {
       struct data value;
       struct node* next;
       struct node* prev;
    };
    and write only struct data to the disk
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can't save pointers to a binary file (well, you can, but they are completely useless when you read them back, since the memory allocated last time won't be there when you load it back in).

    So the principle is that you either:
    1. Retain the tree structure by storing an offset to the block of data in the file.
    2. Reconstruct the tree on loading.
    3. Use a different method to store the data in the application.

    #2 is probably the easiest to both implement and use.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    when you say .bin file isn't a good way to store data from a double linked list, you also mean a .txt file?

    how do i accomplish that?
    "Artificial Intelligence usually beats natural stupidity."

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    struct data
    {
    	int idPaciente;
    	int idMedico;
    	int especialidade;
    	int horaConsulta;
    	int faixaEtaria;
    	int duracao;
    
    	int tipoConsulta;
    	char desc[100];
    
    	Data dataConsul;
    	Hora horaMarcacao;
    } test;
    fprintf(fp,"%d,%d,%d,%d,%d,%d,%d,%s\n",
    	   test.idPaciente,
    	   ...
    	   test.desc);
    for each node
    when you read it line by line and use sscanf to fill node data back...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    ok...first step done :-D

    i've managed to save the data into the .txt file:
    Code:
    void guardarConsultas(pnoCons p)
    {
    	FILE *f;
    
    	f = fopen("consultas.txt", "w");
    
    	while(p != NULL)
    	{
    		fprintf(f, "%d\n%d\n%d\n%d\n%d\n%d\n%s%d\n%d %d %d\n%d %d %d\n",
    				p->idPaciente, p->faixaEtaria, p->tipoConsulta, p->horaConsulta, 
    				p->especialidade, p->duracao, p->desc, p->idMedico, 
    				p->horaMarcacao.hora, p->horaMarcacao.min, p->horaMarcacao.seg,
    				p->dataConsul.dia, p->dataConsul.mes, p->dataConsul.ano);
    
    		p = p->prox;
    	}
    
    	fclose(f);
    }
    consultas.txt
    123456
    1
    2
    2
    5
    40
    desc
    123456
    2 37 31
    17 4 2008
    but know the tricky part...load the data :-(

    how do i do that know?
    "Artificial Intelligence usually beats natural stupidity."

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you used
    Code:
    fprintf(f, "%d\n%d\n%d\n%d\n%d\n%d\n%s%d\n%d %d %d\n%d %d %d\n"
    to write it, why wouldn't you use
    Code:
    fscanf(f, "%d\n%d\n%d\n%d\n%d\n%d\n%s%d\n%d %d %d\n%d %d %d\n"
    to read it? (Answer: it depends on whether the string desc can have spaces in it.)

  8. #8
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    humm...
    this is what i have:
    Code:
    void carregarDuracao(pnoCons p)
    {
    	FILE *f;
    
    	f = fopen("duracao.txt", "r");
    
    	while(f != EOF)
    	{
    		fscanf(f, "%d\n%d\n%d\n%d\n%d\n%d\n%s%d\n%d %d %d\n%d %d %d\n",
    				p->idPaciente, p->faixaEtaria, p->tipoConsulta, p->horaConsulta, 
    				p->especialidade, p->duracao, p->desc, p->idMedico, 
    				p->horaMarcacao.hora, p->horaMarcacao.min, p->horaMarcacao.seg,
    				p->dataConsul.dia, p->dataConsul.mes, p->dataConsul.ano);
    	}
    
    	fclose(f);
    }
    how do i know when i reach the end of the .txt file?
    and how do i insert the data into the double linked list?
    "Artificial Intelligence usually beats natural stupidity."

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by IndioDoido View Post
    how do i know when i reach the end of the .txt file?
    There is such a thing as feof. You can also check the return value of fscanf. In either case "you won't know until you try" -- you will have to try to read, and fail.
    and how do i insert the data into the double linked list?
    Put a node into the list and then read the data into it -- notice that you're function here takes a pointer to node, so that's where the information is going to go.

  10. #10
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    this is what i've done:
    Code:
    int main(void)
    {	
    	...
    	pnoCons consultas = NULL;
    	...
    
    	consultas = carregarConsultas(consultas); //<---- load?
    
    	...
    	return 0;
    }
    load function:
    Code:
    pnoCons carregarConsultas(pnoCons p)
    {
    	FILE *f;
    	
    	pnoCons novo, aux;
    
    	f = fopen("consultas.txt", "r");
    
    	while(feof(f) != 0)
    	{
    		novo = malloc(sizeof(noCons));
    
    		fscanf(f, "%d\n%d\n%d\n%d\n%d\n%d\n%s\n%d\n%d %d %d\n%d %d %d\n",
    				&novo->idPaciente, &novo->faixaEtaria, &novo->tipoConsulta, &novo->horaConsulta, 
    				&novo->especialidade, &novo->duracao, &novo->desc, &novo->idMedico, 
    				&novo->horaMarcacao.hora, &novo->horaMarcacao.min, &novo->horaMarcacao.seg,
    				&novo->dataConsul.dia, &novo->dataConsul.mes, &novo->dataConsul.ano);
    
    		if(p==NULL)
    			p=novo;
    		else 
    		{
    			aux = p;
    			while(aux->prox != NULL)
    				aux = aux->prox;
    
    			aux->prox = novo;
    			novo->prev = aux;
    			novo->prox = NULL;
    		}
    	}
    	fclose(f);
    
    	return p;
    }
    this isn't working :-(
    "Artificial Intelligence usually beats natural stupidity."

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    do not use feof to control loops - read FAQ http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    check the return value of scanf and see how many items it could read, I suppose the problem is &#37;s format, probably you need to replace it with %[^\n] format
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by IndioDoido View Post
    when you say .bin file isn't a good way to store data from a double linked list, you also mean a .txt file?

    how do i accomplish that?
    No, I actually meant that you CAN NOT save POINTERS in from a linked list or tree in a file - no matter what the format of the file is.

    Whether you choose to save binary or text format has nothing to do with that. As suggested, fscanf() is no good for reading strings with spaces - and long format strings for fscanf/fprintf are hard to maintain, so it may be easier to write as:
    Code:
        fprintf("%d\", firstthing);
        fprintf("%d\n", secondthing);
    and
    Code:
        fscanf("%d\", &firstthing);
        fscanf("%d\n", &secondthing);
    That way, you also have a choice of using a different method for reading strings, such as fgets() (you can still use fprintf() to write a string).

    The choice of binary or text is pretty much dependant on your desire to be portable. If this is for a single architecture (architecture here is the combination of OS, Processor type and compiler), then writing a binary "lump" is efficient. But if you want portability, then writing the data as text is much easier to deal with (and the added benefit[1] that you can read the data with a text-editor).

    [1] Of course, this is SOMETIMES a drawback - for example, if you store sensitive data in the file - but then you probably should use some PROPER encryption, not just make the file binary, as many text-editors will still show you the text content (names for example) of a binary file, just that the OTHER data (integers, floats) are a bit harder to read.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. Replies: 13
    Last Post: 04-26-2008, 08:29 PM
  3. Using a linked list globally and File I/O
    By Leftos in forum C Programming
    Replies: 46
    Last Post: 01-07-2008, 11:21 AM
  4. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM