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.
#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;
}