DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   file input and output (http://www.daniweb.com/forums/thread1107.html)

steve1_rm Sep 15th, 2003 1:30 am
file input and output
 
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);


All times are GMT -4. The time now is 6:49 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC