Pregunta

I (as always) am having a bit of trouble to write and read from a linked list. The fact that it is saved to a binary file confueses me. I tried to code a simple program, with only one simple structure with two variables, and writing and reading it to disk, just to check whether I was misusing fread() or fwrite(). However, I was not. The program ran smoothly. The trouble starts when I start adding the pointers and the nodes to the linked list. I've checked stackoverflow.com, cplusplus.com, cprograming.com, and, from what I see, my program has nothing wrong to it. Tried debugging, nothing. The compiler doesn't complain at all.

The source I included below is very simple: contains a structure with two variables, name and code, like a Clients Control. The code is given automatically by the program and the user inputs the name. The program should then save the data, and keep going. However, it doesn't get past the fir->sacode = 1 assignment. Any tips would be greatly appreciated.

#include <stdio.h>

void save(void);
int load(void);

struct solic {
    char name[10];
    int sacode;
    struct solic *next;
};

struct solic *cur, *fir;
int sacode = 1;

int main()
{
    if(load() == 0)
        printf("Load Successful!\n");

    char cho1;
    fir->sacode = 1;
    cur=fir;
    while(1){
        cur->sacode = sacode;
        printf("Client code is %04d",cur->sacode);
        printf("Enter client name: ");
        scanf("%s",cur->name);
        save();
        printf("Saved!\n");
        printf("Press '1' to enter another client, or 'Enter' to save and exit.\n");
        scanf("%c",&cho1);
        if(cho1 == '1')
        {
            cur->next = (struct solic *)malloc(sizeof(struct solic));
            cur = cur->next;
            sacode++;
            cur->sacode = sacode;
            continue;
        }
        else if(cho1 == '\r');
            break;
    }

    return(0);
}

void save(void)
{
    FILE *pFile;
    pFile = fopen("db.dat","w");
    while(cur != NULL)
    {
        fwrite(cur->name,sizeof(cur->name),1,pFile);
        fwrite(&cur->sacode,sizeof(int),1,pFile);
        cur = cur->next;
    }
    fclose(pFile);
}

int load(void)
{
    int size;
    char buffer[10];

    FILE *pFile;
    pFile = fopen("db.dat","r");
    if(pFile == NULL)
        return(1);

    size = fseek(pFile,0,SEEK_END);
    rewind(pFile);
    cur = fir;

    while(size >= ftell(pFile))
    {
        fread(cur->name,sizeof(cur->name),1,pFile);
        fread(buffer,sizeof(int),1,pFile);
        cur->sacode = atoi(buffer);
        sacode++;
        cur->next = (struct solic *)malloc(sizeof(struct solic));
        cur = cur->next;
    }
    cur->next = NULL;
    return(0);
}
¿Fue útil?

Solución

  1. The first time you run this program, load() will fail because there is no data. That means that fir will not have been initialized, so fir->sacode = 1 will try to update some random location in memory. Before you store anything in fir, you will need to allocate a new node to it with malloc().

  2. Your save() function writes a new db.dat file each time it runs, starting from the cur pointer. But each time it's called, cur points to the last element in the list. You will need either to open this file for appending, so it doesn't write any data that's already there, or loop through the entire list.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top