简体   繁体   中英

fread failure issue (IOS 64bit environment)

My problem is that, below code is working fine on another platforms, but on iOS 64bit it isn't.

details are in following code :

    //FILE* f = fopen( .. ); // f is opened and already be used successfully. 
    //Size of target file is near 50mb

    fseek(f, 0, SEEK_END);
    // print ftell(f) -> 53394002
    fseek(f, -1024, SEEK_END);
    // print ftell(f) -> 53392978

    fread(buf, 1, 1024, f); // returns 0.
    ferror(f) // returns 3. 

    // print ftell(f) -> 53392978
    fseek(f, 0, SEEK_END);
    // print ftell(f) -> 53394002

when I tried to use fgetc() (for just test), result was same.

one of strange thing is that, return value 3 of ferror(). I heart the value means ESRCH("No such process"), and almost of documents what i found says the value is not related with file reading task.

Could give me some advise please?

The return value from ferror() is not the error number; it is zero for no error and non-zero for an error. If you want to know the error number, check errno . It would be better to show the full code including the tests on the return values from fseek() , and so on.

I can't reproduce your problem on my Mac. I created a big file which is the same size as yours:

$ ls -l big.file
-rw-r--r--  1 jleffler  staff  53394002 Oct  5 19:57 big.file
$

Code

#include <stdio.h>

int main(int argc, char **argv)
{
    const char *filename = "big.file";
    if (argc > 1)
        filename = argv[1];
    FILE *f = fopen(filename, "r");
    if (f == 0)
    {
        perror(filename);
        return 1;
    }

    if (fseek(f, 0, SEEK_END) != 0)
        perror("fseek END 1");
    printf("EOF %ld\n", ftell(f));
    if (fseek(f, -1024, SEEK_END) != 0)
        perror("fseek END 2");
    printf("POS %ld\n", ftell(f));

    char buf[1024];
    size_t n = fread(buf, 1, 1024, f);  // OP reports returns 0.
    printf("N = %zu\n", n);
    int r = ferror(f);                  // OP reports returns 3. 
    printf("r = %d\n", r);

    printf("POS %ld\n", ftell(f));
    if (fseek(f, 0, SEEK_END) != 0)
        perror("fseek END 3");
    printf("POS %ld\n", ftell(f));

    fclose(f);
    return 0;
}

This is not great code: it doesn't abandon ship on detecting errors ( perror() returns after printing); it does not show what data is read so it can be compared with what's in the file (which was generated with a random data generator and then truncated to the precise length of your file with GNU truncate )

Compilation and run

$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror bf19.c -o bf19  
$ ./bf19
EOF 53394002
POS 53392978
N = 1024
r = 0
POS 53394002
POS 53394002
$

This indicates that the code works on my Mac running macOS Sierra 10.12 and GCC 6.2.0. Your problem, therefore, is in code that you've not shown. Adapt your code so it can be compiled and run and show a minimal test case like this one that reproduces the problem. For guidance on how to do it, read about:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM