c - Platform-dependent behavior of fread with large nitems parameter -
i created test program below in attempt debug issue in larger system. program attempts fread
large chunk of data small file, i've found behavior of program differs across platforms.
expected behavior
the behavior expect, , see on platforms (e.g., mac os x, centos 6.4), program read n bytes (the size of file) , terminate eof
condition:
read 2 bytes. ok. eof
anomalous behavior
on platforms (e.g., centos 6.6), program reports 0 bytes read , fails error:
read 0 bytes. noooooooooooooo!!!! error: operation not permitted
on these platforms, can code work expected reducing chunk
size "enough", i'm not sure how determine acceptable size programmatically...
questions
- am using
fread
correctly? - is there more portable approach should using?
- any suggested workarounds?
code
#include <stdlib.h> #include <stdio.h> #include <string.h> int main(int argc, char* argv[]) { file* file = fopen("test.txt", "rb"); size_t chunk = 0x7fffffff; char* buf = malloc(chunk); if (buf == null) { printf("malloc failed\n"); return 1; } int count = fread(buf, 1, chunk, file); printf("read %i bytes.\n", count); int eof = feof(file); if (eof) { printf("ok. eof\n"); } int err = ferror(file); if (err) { printf("noooooooooooooo!!!! error: %s\n", strerror(err)); return 1; } free(buf); return 0; }
for test, created test.txt echo "a" >test.txt
.
Comments
Post a Comment