file input and output

Reply

Join Date: Sep 2003
Posts: 1
Reputation: steve1_rm is an unknown quantity at this point 
Solved Threads: 0
steve1_rm steve1_rm is offline Offline
Newbie Poster

file input and output

 
0
  #1
Sep 15th, 2003
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

  1. //Structure that Holds details for all the customers
  2. struct freight{
  3. char name[20];
  4. char address[40];
  5. char goods[30];
  6. int quantity;
  7. char destination[30];
  8. } customerDetails[20];
  9.  
  10.  
  11.  
  12. //Code for opening the file
  13. if((fileCustomers = fopen(fileName, "wb")) == NULL)
  14. printf("\nCannot save this record");
  15.  
  16. //Code for writing to the file
  17. for(i=0; i<=recordNumber; i++){
  18. if(fwrite(&customerDetails[i], sizeof(struct freight),1,fileCustomers) != 1){
  19. printf("\nError saving customer records");
  20. exit(1);
  21. }
  22. }
  23. fclose(fileCustomers);
  24.  
  25. //code for opeing the file
  26. if((fileCustomers = fopen(fileName, "rb")) == NULL){
  27. printf("\nCannot not find customer records on disk");
  28. printf("\nOr file may not have been created yet");
  29. }
  30.  
  31. //Code for reading from the file.
  32. for(i=0; i<=2; i++){
  33. if(fread(&customerDetails[i],sizeof(struct freight),1,fileCustomers) != 1){
  34. printf("\nError opening customer records");
  35. exit(1);
  36. }
  37. }
  38. fclose(fileCustomers);
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC