Hello
C problem
I am having problems saving a structure to a text file. My structure is below.
What is the best way to save this to a file and open it from a file and display it on the screen. I have tried several ways but can't get it to work propertly.
I have tried using fread and fwrite, but having problems with it.
This is what l have done so far. It is bits of code that does not work
//Structure that Holds details for all the customers
struct freight{
char name[20];
char address[40];
char goods[30];
int quantity;
char destination[30];
} customerDetails[20];
//Code for opening the file
if((fileCustomers = fopen(fileName, "wb")) == NULL)
printf("\nCannot save this record");
//Code for writing to the file
for(i=0; i<=recordNumber; i++){
if(fwrite(&customerDetails[i], sizeof(struct freight),1,fileCustomers) != 1){
printf("\nError saving customer records");
exit(1);
}
}
fclose(fileCustomers);
//code for opeing the file
if((fileCustomers = fopen(fileName, "rb")) == NULL){
printf("\nCannot not find customer records on disk");
printf("\nOr file may not have been created yet");
}
//Code for reading from the file.
for(i=0; i<=2; i++){
if(fread(&customerDetails[i],sizeof(struct freight),1,fileCustomers) != 1){
printf("\nError opening customer records");
exit(1);
}
}
fclose(fileCustomers);