c - Created file has nothing in it -


so suppose make program persistent when opened , closed assume have file, program payroll program uses structs. 2 questions here 1. when comes type binary files easier? hear txt files complicated not sure why. 2. here code runs without error when go file nothing written inside. these 2 structs

typedef struct{     int day;     int month;     int year; }date;  typedef struct{     char name[100];     int age;     float hrsworked;     float hrlywage;     float regpay;     float otpay;     float totalpay;     date paydate;  }payroll; 

and code

void backup(payroll employee[], long int *pcounter) {     file *record = fopen_s(&record, "c:\\record.bin", "wb");     if (record != null){         fwrite(employee, sizeof(payroll), 1, record);         fclose(record);     } 

employee has stuff in structs know not empty if 1 explain parameters fwrite that'd great!

you seem mixing usage of longtime-standard fopen() function usage of new-in-c11 fopen_s() function. latter returns error code, not stream pointer. overwriting stream pointer sets via first argument error code.

if program opens file, returns 0. after setting value of record, record compares equal null (in microsoft's c implementation), don't attempt write. if caught case , printed diagnostic, have had clue (albeit misleading one).

you should this:

void backup(payroll employee[], long int *pcounter) {     file *record;     errno_t result = fopen_s(&record, "c:\\record.bin", "wb");      if (result == 0) {         fwrite(employee, sizeof(payroll), 1, record);         fclose(record);     }     /* ... */ } 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -