fread, fwrite issues

Reply

Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

fread, fwrite issues

 
0
  #1
Aug 10th, 2006
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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Re: fread, fwrite issues

 
0
  #2
Aug 11th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,161
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1438
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: fread, fwrite issues

 
0
  #3
Aug 11th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: fread, fwrite issues

 
0
  #4
Aug 11th, 2006
so you're allowed to change the state of the file by pointing the pointer elsewhere?
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: fread, fwrite issues

 
0
  #5
Aug 11th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,161
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1438
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: fread, fwrite issues

 
0
  #6
Aug 11th, 2006
read the description of the open flags -- one of the allows both read and writes and your program only opens the file once.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,319
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 230
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: fread, fwrite issues

 
0
  #7
Aug 11th, 2006
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
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: fread, fwrite issues

 
0
  #8
Aug 12th, 2006
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
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC