| | |
fread, fwrite issues
![]() |
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Solved Threads: 0
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.
C Syntax (Toggle Plain Text)
#include <stdio.h> #define MAX 1 typedef struct { short int key; char name[21]; char symbol[6]; float price; float high; float low; short int ratio; } STOCK; void get_data(STOCK *); int main(void) { FILE *fp; // STOCK data[MAX]; STOCK data[MAX] = { { {1000}, {"nasdaq"}, {"NAS"}, {100}, {100}, {50}, {2} } }; if (!(fp = fopen("stockdata.dat", "rb"))) { // get_data(data); FILE *temp = fopen("stockdata.dat", "wb"); fwrite(data, sizeof(STOCK), MAX, temp); fclose(temp); } STOCK test[MAX]; fread(test, sizeof(STOCK), MAX, fp); printf("%hd", test[0].key); return 0; }
•
•
Join Date: Aug 2005
Posts: 148
Reputation:
Solved Threads: 6
If you want to write array of structures in file you should use loop to store each member. Coinsider this code:
Now, I think you'll know what to do...
C Syntax (Toggle Plain Text)
#include <string.h> #define LEN 5 typedef struct Test { int x; double y; }Test; int main(void) { Test array[LEN]; Test res[LEN]; FILE* fp; int i; /*Open for writing*/ fp = fopen("test.bin","wb"); for (i = 0; i <LEN; i++) { array[i].x = i; array[i].y = i * 2.5; fwrite(&array[i], sizeof(Test), 1, fp); } fclose(fp); /* Open for reading*/ fp = fopen("test.bin", "rb"); for (i = 0; i <LEN; i++) { fread(&res[i], sizeof(Test), 1, fp); } for (i = 0; i <LEN; i++) { printf ("%d %g\n", res[i].x, res[i].y); } fclose(fp); return 0; }
Last edited by Micko; Aug 11th, 2006 at 2:01 am.
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.
This works for me.
C Syntax (Toggle Plain Text)
#include <stdio.h> #define MAX 1 typedef struct { short int key; char name[21]; char symbol[6]; float price; float high; float low; short int ratio; } STOCK; void get_data(STOCK *); int main(void) { FILE *fp; STOCK data[MAX] = { { {1000}, {"nasdaq"}, {"NAS"}, {100}, {100}, {50}, {2} } }; // if the file does not exist, then create it if(!(fp = fopen("stockdata.dat", "rb")) ) { if(fp = fopen("stockdata.dat", "wb")) { fwrite(data, sizeof(STOCK), MAX, fp); fclose(fp); fp = fopen("stockdata.dat", "rb"); } else { printf("can't write to file\n"); return 1; } } STOCK test[MAX]; fread(test, sizeof(STOCK), MAX, fp); fclose(fp); printf("%hd\n", test[0].key); return 0; }
Last edited by Ancient Dragon; Aug 11th, 2006 at 8:13 am.
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Solved Threads: 0
How would I copy data from one file directly to another? I tried this syntax:
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.
C Syntax (Toggle Plain Text)
void disp_all(FILE *fp) { FILE *ftemp = fopen("tempdata.dat", "wb"); fwrite(fp, sizeof(STOCK), 1, ftemp); fclose(ftemp); ftemp = fopen("tempdata.dat", "rb"); STOCK temp[MAX]; fread(temp, sizeof(STOCK), 1, ftemp); printf("%hd\n", temp[0].key); rewind(fp); }
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.
read the description of the open flags -- one of the allows both read and writes and your program only opens the file once.
•
•
•
•
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.
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
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Quick search & replace with c-strings?
- Next Thread: Segmenting a file
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking highest homework i/o inches include incrementoperators input interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft mqqueue mysql number odf open openwebfoundation owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault send sequential shape socket socketprograming socketprogramming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h






