Search code examples
cstructlinked-listbinaryfilesfwrite

Writing a linked list to binary file(C)


So I have set up a linked list and read data from a file, done some calculation and manipulation and now I want to store the new list into a binary file.

Here is my struct setup:

typedef struct Comp{
    char name[5];
    char node1[5], node2[5];
    float value;    //value
}ComponentType;

typedef struct ListNodeT{

    ComponentType Component;
    float voltage, power, current;
    struct ListNodeT *nextPtr;
}ListNodeType;

In a separate function I am trying to write to a a file:

FILE *filePtr;

char fileName[13] = "SaveData.bin";
filePtr = fopen(fileName, "wb");
int index = 0;

while (CircuitData != NULL)
{
    fwrite(CircuitData, sizeof(ListNodeType), 1, filePtr);
    CircuitData = CircuitData->nextPtr;
    index++;
}

The above code is not working so now my question is, can I write to the file using a single fwrite(CircuitData, sizeof(ListNodeType), 1, filePtr) or should I write each component separately as follows:

fwrite(CircuitData->Component.name, sizeof(CircuitData->Component.name), 1, filePtr);

How should I be doing this? My second question is, how would I read this binary file again back into the Struct?


Solution

  • Well, you won't be able to use the pointer-values from a file in a new invocation of the program, so you'll need to do something about that. You could make a copy before printing and zero-out the pointer; then when reading, you'll need to assign the correct pointer values for the newly allocated structs.

    I'd probably read them with a recursive function. Something like this:

    ListNodeType *read_list_from_file (FILE *fileptr) {
        ListNodeType node;
        ListNodeType *nodeptr;
        if (fread(&node, sizeof(node), 1, fileptr) == sizeof(node)) {
            nodeptr = malloc(sizeof(node));
            //TODO: handle malloc failure
            memcpy(nodeptr, &node, sizeof(node);
            nodeptr->nextPtr = read_list_from_file(fileptr);
            return nodeptr;
        } else {
            return NULL;
        }
    }