简体   繁体   中英

Writing data to binary file

I am attempting to create a binary .dat file so i attempted this by

#include<stdio.h>
struct employee {
    char firstname[40];
    char lastname[40];
    int id;
    float GPA;
 };
 typedef struct employee Employee;

void InputEmpRecord(Employee *);
void PrintEmpList(const Employee *);
void SaveEmpList(const Employee *, const char *);
int main()
{
    Employee EmpList[4];
    InputEmpRecord(EmpList);
    PrintEmpList(EmpList);
    SaveEmpList(EmpList, "employee.dat");
    return 0;
}

void InputEmpRecord(Employee *EmpList)
{
    int knt;
    for(knt = 0; knt < 4; knt++) {
        printf("Please enter the data for person %d: ", knt + 1);
        scanf("%d %s %s %f", &EmpList[knt].id, EmpList[knt].firstname,EmpList[knt].lastname, &EmpList[knt].GPA);
    }
}

void PrintEmpList(const Employee *EmpList)
{
    int knt;
    for(knt = 0; knt < 4; knt++) {
        printf("%d %s %s %.1f\n", EmpList[knt].id, EmpList[knt].firstname,EmpList[knt].lastname, EmpList[knt].GPA);
    }
}

void SaveEmpList(const Employee *EmpList, const char *FileName)
{
    FILE *p;
    int knt;
    p = fopen(FileName, "wb"); //Open the file
    fwrite(EmpList, sizeof(Employee), 4, p); //Write data to binary file
    fclose(p);
}

I give it the input:

10 John Doe 64.5
20 Mary Jane 92.3
40 Alice Bower 54.0
30 Jim Smith 78.2

So the printf statement works and prints the correct information to the screen but the employee.dat file that is created is just random symbols. The file does not currently exist so the program is creating it.

You are writing the entire list of employee records 4 times. Instead of

for(knt = 0; knt < 4; knt++) {
    if(EmpList[knt].firstname != NULL && EmpList[knt].lastname != NULL) {
         fwrite(EmpList, sizeof(Employee), 4, p);
    }
}

you can just use:

fwrite(EmpList, sizeof(Employee), 4, p);

If you must have the checks, you can use:

for(knt = 0; knt < 4; knt++) {
    if(EmpList[knt].firstname != NULL && EmpList[knt].lastname != NULL) {
         fwrite(EmpList+knt, sizeof(Employee), 1, p);
    }
}

Expanding upon @adamdc78 and my comments:

The data is being written to the file in 1's and 0's. It's how you are reading it that is causing you to see a "bunch of random symbols". A text editor expects data to be encoded in either ASCII or Unicode formatting (among others).

For ASCII , each byte of raw binary represents a character. This encoding is sufficient for a normal English language and its punctuation, but not for all the symbols used worldwide.

So they came up with Unicode , which uses a variable byte length encoding to capture all of the language symbols used worldwide (a lot more than just AZ,az.)

Hope this helps.

I'll add some links to reading material in a second. Also note: someone will probably pedantically comment that something I just wrote is not quite accurate. That's why I'm referencing the links.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM