944,031 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 19219
  • C RSS
Aug 10th, 2006
0

fread, fwrite issues

Expand Post »
One of the things I can't get working is reading and writing a structure to and from a file in binary. I'm sure that I'm using fread and fwrite correctly. However, I keep getting crashes which I cannot find ways to debug.

  1. #include <stdio.h>
  2.  
  3. #define MAX 1
  4.  
  5. typedef struct {
  6. short int key;
  7. char name[21];
  8. char symbol[6];
  9. float price;
  10. float high;
  11. float low;
  12. short int ratio;
  13. } STOCK;
  14.  
  15. void get_data(STOCK *);
  16.  
  17. int main(void) {
  18.  
  19. FILE *fp;
  20. // STOCK data[MAX];
  21. STOCK data[MAX] = { {
  22. {1000}, {"nasdaq"}, {"NAS"}, {100}, {100}, {50}, {2}
  23. }
  24. };
  25.  
  26. if (!(fp = fopen("stockdata.dat", "rb"))) {
  27. // get_data(data);
  28. FILE *temp = fopen("stockdata.dat", "wb");
  29. fwrite(data, sizeof(STOCK), MAX, temp);
  30. fclose(temp);
  31. }
  32.  
  33. STOCK test[MAX];
  34. fread(test, sizeof(STOCK), MAX, fp);
  35. printf("%hd", test[0].key);
  36.  
  37. return 0;
  38. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
Aug 11th, 2006
0

Re: fread, fwrite issues

If you want to write array of structures in file you should use loop to store each member. Coinsider this code:
  1. #include <string.h>
  2. #define LEN 5
  3.  
  4. typedef struct Test
  5. {
  6. int x;
  7. double y;
  8. }Test;
  9.  
  10.  
  11. int main(void)
  12. {
  13. Test array[LEN];
  14. Test res[LEN];
  15. FILE* fp;
  16. int i;
  17. /*Open for writing*/
  18. fp = fopen("test.bin","wb");
  19. for (i = 0; i <LEN; i++)
  20. {
  21. array[i].x = i;
  22. array[i].y = i * 2.5;
  23. fwrite(&array[i], sizeof(Test), 1, fp);
  24. }
  25. fclose(fp);
  26. /* Open for reading*/
  27. fp = fopen("test.bin", "rb");
  28. for (i = 0; i <LEN; i++)
  29. {
  30. fread(&res[i], sizeof(Test), 1, fp);
  31. }
  32. for (i = 0; i <LEN; i++)
  33. {
  34. printf ("%d %g\n", res[i].x, res[i].y);
  35. }
  36. fclose(fp);
  37.  
  38. return 0;
  39. }
Now, I think you'll know what to do...
Last edited by Micko; Aug 11th, 2006 at 2:01 am.
Reputation Points: 55
Solved Threads: 6
Junior Poster
Micko is offline Offline
148 posts
since Aug 2005
Aug 11th, 2006
0

Re: fread, fwrite issues

I know its possible to write binary files because I've done it hundreds of times, and so has almost everyone else.

This works for me.

  1. #include <stdio.h>
  2.  
  3. #define MAX 1
  4.  
  5. typedef struct {
  6. short int key;
  7. char name[21];
  8. char symbol[6];
  9. float price;
  10. float high;
  11. float low;
  12. short int ratio;
  13. } STOCK;
  14.  
  15. void get_data(STOCK *);
  16.  
  17. int main(void) {
  18.  
  19. FILE *fp;
  20. STOCK data[MAX] = { {
  21. {1000}, {"nasdaq"}, {"NAS"}, {100}, {100}, {50}, {2}
  22. }
  23. };
  24. // if the file does not exist, then create it
  25. if(!(fp = fopen("stockdata.dat", "rb")) )
  26. {
  27. if(fp = fopen("stockdata.dat", "wb")) {
  28. fwrite(data, sizeof(STOCK), MAX, fp);
  29. fclose(fp);
  30. fp = fopen("stockdata.dat", "rb");
  31. }
  32. else
  33. {
  34. printf("can't write to file\n");
  35. return 1;
  36. }
  37. }
  38.  
  39. STOCK test[MAX];
  40. fread(test, sizeof(STOCK), MAX, fp);
  41. fclose(fp);
  42. printf("%hd\n", test[0].key);
  43.  
  44. return 0;
  45. }
Last edited by Ancient Dragon; Aug 11th, 2006 at 8:13 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Aug 11th, 2006
0

Re: fread, fwrite issues

so you're allowed to change the state of the file by pointing the pointer elsewhere?
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
Aug 11th, 2006
0

Re: fread, fwrite issues

How would I copy data from one file directly to another? I tried this syntax:

  1. void disp_all(FILE *fp) {
  2.  
  3. FILE *ftemp = fopen("tempdata.dat", "wb");
  4.  
  5. fwrite(fp, sizeof(STOCK), 1, ftemp);
  6. fclose(ftemp);
  7. ftemp = fopen("tempdata.dat", "rb");
  8.  
  9. STOCK temp[MAX];
  10. fread(temp, sizeof(STOCK), 1, ftemp);
  11. printf("%hd\n", temp[0].key);
  12.  
  13. rewind(fp);
  14. }

and it doesn't seem to work

fp is in "rb" mode. I want to write the file that fp is pointing to directly into a temporary file that ftemp is pointing to.
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
Aug 11th, 2006
0

Re: fread, fwrite issues

read the description of the open flags -- one of the allows both read and writes and your program only opens the file once.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Aug 11th, 2006
0

Re: fread, fwrite issues

Quote originally posted by Ancient Dragon ...
I know its possible to write binary files because I've done it hundreds of times, and so has almost everyone else.
Writing structures as text files may be preferred for portability and human read/writeability.
http://c-faq.com/struct/io.html
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 12th, 2006
0

Re: fread, fwrite issues

ok, i read the website that ancient dragon posted up and it was pretty helpful. I managed to get the file copying function of my program to work
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006

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: Quick search & replace with c-strings?
Next Thread in C Forum Timeline: Segmenting a file





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


Follow us on Twitter


© 2011 DaniWeb® LLC