fdopen()
use file descriptor to file pointer:
The fdopen()
function associates a stream with a file descriptor. File descriptors are obtained from open()
, dup()
, creat()
, or pipe()
, which open files but do not return pointers to a FILE
structure stream. Streams are necessary input for almost all of the stdio library routines.
FILE* fp = fdopen(fd, "w");
this example code may help you more as you want to use fprintf()
:
int main(){
int fd;
FILE *fp;
fd = open("my_file",O_WRONLY | O_CREAT | O_TRUNC);
if(fd<0){
printf("open call fail");
return -1;
}
fp=fdopen(fd,"w");
fprintf(fp,"we got file pointer fp bu using File descriptor fd");
fclose(fp);
return 0;
}
Notice:
fdopen
must be compatible with the file descriptor ones. – Entasis