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?