Search code examples
cfreadmingw-w64msys

fread() under mingw not working properly


I have written a C application under Linux with GTK. A friend wanted to test it under Windows. So we compiled it using MinGW64.

The GUI and everything looks/works as it should. However, the fread() call does not work.

read = fread(workbuff, sizeof(char), rec_data_length, bin_file);

    if (read != rec_data_length) {
        /* Here is some error handling */
    }

rec_data_length is 608. I ensured that the file is not corrupted and that these 608 bytes are available. The function returns 87.

Can someone explain this to me? Why does it work under Linux but not under Windows?


Solution

  • The problem with reading from this file was, that I opened a binary file with

    fopen("foo", "r");
    

    This worked fine under Linux. But on Windows I had to change it to

    fopen("foo", "rb");
    

    This solution works on both systems and behaves now as expected.