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.
#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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Offline 21,953 posts
since Aug 2005