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!

7
  • 1
    while(!feof) is always wrong Commented May 23, 2020 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.) Commented May 23, 2020 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? Commented May 23, 2020 at 18:37
  • Michael, can you please try again with @KamilCuk fix? Commented May 23, 2020 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. Commented May 23, 2020 at 19:03

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.