943,754 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4416
  • C RSS
Sep 15th, 2003
0

file input and output

Expand Post »
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);
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
steve1_rm is offline Offline
1 posts
since Sep 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: capturing text
Next Thread in C Forum Timeline: string to integer array transformation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC