1

I have a struct employee:

typedef struct employee
{
    int name_length;
    char* name;
    float salary;
} Employee;

Now I want to make an array of employees, and get it from a binary file for each employee the name, and salary. (binary file scheme is built like: 6michel7544.564 (length of name, name itself, salary) and so on with unknown number of employees).

I built this function to build the array:

Employee** makeEmpArray(char* fname1, int* size)
{
    FILE* empFile;
    int k = 0;
    Employee** arr; //array of employees
    arr = (Employee**)malloc(sizeof(Employee*));
    assert(arr != NULL);
    empFile = fopen(fname1, "rb");
    if (ferror(empFile))
    {
        printf("Error, cant open file");
        exit(2);
    }
    while (!feof(empFile))
    {
        arr[k] = (Employee*)malloc(sizeof(Employee));
        assert(arr[k]);
        fread(&(arr[k]->name_length), sizeof(int), 1, empFile); // get length of name 
        arr[k]->name = (char*)malloc(arr[k]->name_length * sizeof(char)); //allocate memory for name in namelength size
        assert(arr[k]->name!=NULL);
        fread(arr[k]->name, sizeof(char), arr[k]->name_length, empFile); //read letter letter (namelen times)
        fread(&arr[k]->salary, sizeof(float), 1, empFile); //reads floeat salary
        k++;
        arr = realloc(arr, (k + 1) * sizeof(Employee*)); //realloc for next employee
        assert(arr != NULL);
    }
    fclose(empFile);
    *size = k;
    return arr;
}

and every thing is going well except the string of the name! I get the name_length perfectly, I get the salary perfectly, and for some reason this line is the problematic:

fread(arr[k]->name, sizeof(char), arr[k]->name_length, empFile);

The name does not read from the file. No error is prompt, and when I watch arr[k]->name when debugging I see <invalid characters in string.> enter image description here

Hope you could help me identify the problem!

  • 1
    [while(!feof) is always wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – KamilCuk May 23 '20 at 18:34
  • Where do the invalid characters appear? After the correct name, like "michel%$!"? Your `name` is not null-terminated. Allocate one char more and then set the char after the actual name to `'\0'`. (I doubt that you can read the salary correctly when reading the name fails.) – M Oehm May 23 '20 at 18:36
  • @KamilCuk How do you explain this then `every thing is going well except the string of the name!! I get the name_length perfectly, I get the salary perfectly, and for some reason this line is the problematic`? – MacOS May 23 '20 at 18:37
  • Michael, can you please try again with @KamilCuk fix? – MacOS May 23 '20 at 18:41
  • @MacOS Ok? `fread` doesn't read base 10 representation of a number, it reads any bytes. OP should convert ASCII characters that represent the number to native machine representation. Typically that's done with `scanf`. The resulting `arr[k]->name` is not zero terminated, `name_length` is not equal `6` but is equal to ASCII character `'6'` and `arr[k]->salary` most probably doesn't contain a valid representation of a `float`. – KamilCuk May 23 '20 at 19:03
  • @KamilCuk This is a good explanation how fread works, thank you. But according to his question, the while loop has been entered. This, however, can not be if `while(!feof)` is always wrong. Don't get me wrong, you are right. I just don't understand how this is possible. But maybe I miss something. – MacOS May 23 '20 at 19:09
  • this is so weird.... I tried allocate an extra one space it didn't help.. i want to send a photo of the debugging so you can see that it read the salary and length but not the name.. still cant figure it out. added a picture of the debugging watch – Michael Bell May 24 '20 at 06:17

1 Answers1

0

I didn't tried my idea, but I think that you should malloc-ate one additional char for the \0 at the end of the name string. In addition, you should clear the just allocated string memory. But I am kind of guessing.

quinzio
  • 40
  • 7