...
| Code Block | ||||
|---|---|---|---|---|
| ||||
FILE *fp;
long file_size;
char *buffer;
struct stat st;
fp = fopen("foo.bin", "rb");
if (fp == NULL) {
/* Handle Error */
}
if ((fstat(fp, &st) != 0) || (!S_ISREG(st.st_mode))) {
/* Handle Error */
}
if (fseek(fp, 0 , SEEK_END) != 0) {
/* Handle Error */
}
file_size = ftell(fp);
if (file_size == -1) {
/* Handle Error */
}
buffer = (char*)malloc(file_size);
if (buffer == NULL) {
/* Handle Error */
}
/* ... */
|
Compliant Solution (Windows, POSIX)
This compliant solution uses fstat() to obtain the size of the binary file.
...
However, 0 is incorrect; the correct value should be 10, as evident when the file is opened in binary mode rather than text mode.
Compliant Solution (Windows, POSIX)
This compliant solution uses fstat() instead to the size of the text file.
...