0

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.

4
  • 1
    Out of curiosity, how are you opening the .dat file to check, in a text editor? Cuz that's gonna be a bunch of random symbols. Commented Mar 24, 2015 at 1:45
  • 3
    It's not random symbols; it's the binary data being interpreted as UTF-8 (or whatever encoding your text editor is using). Commented Mar 24, 2015 at 1:45
  • @RonThompson Yes i am using notepad and i was not aware that random symbols would come up by doing that. I was expecting it to be a bunch of 1s and 0s. Commented Mar 24, 2015 at 1:48
  • @KenWhite when i write EmpList[knt] instead of EmpList i get an error saying Incompatible type for argument 1 of fwrite Commented Mar 24, 2015 at 1:52

2 Answers 2

2

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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 A-Z,a-z.)

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.

2 Comments

Thanks for the answer. I just ran my program again and i am getting mostly "Unicode characters" However for some reason i am getting the names in normal english so it starts with a first name and then its unicode characters and then a last name and so on. why would it do this
I'm assuming that's because the representations for your characters, IE strings, are being decoded properly, whereas your numbers, IE ints and doubles, are not. Also, when writing structs, I believe they are normally padded with zeroes to align the memory properly, this may also affect the decoding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.