My compiler (gcc) throws warnings (not errors!) on the line which declares fp:
int fd = open("filename.dat", O_RDONLY);
FILE* fp = fdopen(fd, "r"); // get a file pointer fp from the file descriptor fd
These are the warnings:
main.c: In function ‘main’:
main.c:606: warning: implicit declaration of function ‘fdopen’
main.c:606: warning: initialization makes pointer from integer without a cast
I do not understand these warnings since the return value of fopen is a FILE*. What is the mistake I am making here?
EDIT: I am including stdio.h (and I am also on Linux).
Short answer: use
-std=gnu99when compiling, the usual standard is non-POSIX and does not havefdopen.Means you have forgot to include the header file which the declaration of
fdopen()resides in. Then an implicit declaration by the compiler occurs – and that means the return value of the unknown function will be assumed to beint– thus the second warning. You have to writeEdit: if you properly include stdio.h, then
fdopen()might not be available on the system you’re targeting. Are you on Windows? This function is POSIX-only.Edit 2: Sorry, I really should have perceived this. C99 means the ANSI C99 standard – and standard C doesn’t force the concept of file descriptors in order to support non-POSIX systems, so it provides
fopen()only.fdopen()is related to file descriptors, so it’s POSIX-only, so it’s not part of standard C99. If you use the-std=gnu99switch for GCC, it gets rid of the standard’s restrictions and lets in the POSIX and GNU-only extensions, essentially fixing your problem.