Question about file I/O from a newbie
Hey all!
Got some problems with basic file I/O on binary file. It seems like i can write to the file bacause its beeing created. But when i type in information, write it to file, and then reading from file, it seems OK. But if i close the program and try to open the file, all that its printed are just jibberish.
// My defines
Code:
#define TY 20
#define FY 50
#define HD 100
#define TI sizeof(struct TestInfo)
#define CO sizeof(struct Course)
My struct:
Code:
struct Course
{
char kurskod[TY];
char kursnamn[TY];
char inst[TY];
char larare[TY];
char kurskommentar[FY];
};
My write to file:
Code:
void write_to_file(struct Course *co)
{
FILE *fp;
fp=fopen("courseinfo.bin", "wb");
fwrite(&co,CO,1, fp);
fclose(fp);
}
My read from file:
Code:
void read_from_file(struct Course *co)
{
FILE *inFile;
if(!(inFile = fopen("courseinfo.bin", "r")))
fread(&co,CO,1,inFile);
fclose(inFile);
}
The way i call them:
Code:
struct Course new_course;
read_from_file(&new_course);
write_to_file(&new_course);
Can anyone see whats from with my functions (or something else for that matter)?
Regards
/Henrik!