How does fread know when the file is over in C?

前端 未结 2 1866
挽巷
挽巷 2021-01-02 03:38

So I\'m not entirely sure how to use fread. I have a binary file in little-endian that I need to convert to big-endian, and I don\'t know how to read the file. Here is what

2条回答
  •  悲哀的现实
    2021-01-02 04:15

    FILE* file = fopen(filename, "rb");
    char buffer[4];
    
    if (!file) {
    /* File was opened successfully. */
    
    /* Attempt to read */
    while (fread(buffer, 1, 4, file) != EOF) {
        /* byte swap here */
    }
    
    fclose(file);
    }
    

提交回复
热议问题