Reputation:
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).
Upvotes: 13
Views: 4472
Reputation:
Short answer: use -std=gnu99
when compiling, the usual standard is non-POSIX and does not have fdopen
.
warning: implicit declaration of function ‘fdopen’
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 be int
- thus the second warning. You have to write
#include <stdio.h>
Edit: 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=gnu99
switch for GCC, it gets rid of the standard's restrictions and lets in the POSIX and GNU-only extensions, essentially fixing your problem.
Upvotes: 16
Reputation: 20393
#define _XOPEN_SOURCE 600
#include <stdio.h>
This conforms perfectly with strict c99
gcc -std=c99 -pedantic -Wall -Wextra -Werror
Upvotes: 2
Reputation: 10096
There's a good explanation for the compiler's diagnostic in @H2CO3's answer, so let's only look on the why of things: if you're using glibc (and you probably are), certain POSIX functions may require specific feature test macros to show up.
In particular, you may need to put the following line:
#define _POSIX_SOURCE
// or #define _XOPEN_SOURCE
before
#include <stdio.h>
Certain compilers (such as gcc) also have command line options to the same effect (all the gnu*
standards options in gcc).
Upvotes: 0
Reputation: 370415
The fdopen
function is not part of the C standard and is not available as part of the standard headers if you compile in standard C mode. So you either need to use -std=gnu99
instead of -std=c99
to compile your source or declare the function yourself.
Upvotes: 0
Reputation: 15989
You are not including #include <stdio.h>
in C the compiler therefore "guesses" the declaration of the function you're trying to call. (Taking the parameters you've based and using int as return value). Usually you don't want such guesses therefore the compiler warns you.
Solution: Add proper #include
s.
Upvotes: 1