...
| Code Block | ||||
|---|---|---|---|---|
| ||||
FILE *fp;
off_t file_size;
char *buffer;
struct stat stbuf;
int fd;
fd = open("foo.bin", O_RDONLY);
if (fd == -1) {
/* Handle Error */
}
if ((fstat(fd, &st) != 0) || (!S_ISREG(st.st_mode))) {
/* Handle Error */
}
file_size = stbuf.st_size;
buffer = (char*)malloc(file_size);
if (buffer == NULL) {
/* Handle Error */
}
/* ... */
|
Compliant Solution (
...
Windows)
Windows provides an _fstat() function, which behaves similarly to POSIX fstat().
| Code Block | ||||
|---|---|---|---|---|
| ||||
FILEint *fpfd; long file_size; char *buffer; struct _stat stbuf; int fd; fd = open( _sopen_s(&fd, "foo.bin", _O_RDONLY, _SH_DENYRW, _S_IREAD); if (fd == -1) { /* Handle Error */ } fpfile_size = fdopen_filelength(fd, "rb"); if (fp == NULL) { /* Handle Error */ } if (_fstat(fd, &stbuf)file_size == -1) { /* Handle Error */ } file_size = stbuf.st_size; buffer = (char*)malloc(file_size); if (buffer == NULL) { /* Handle Error */ } /* ... */ |
...