Function name : fdopen
Header file : <io.h>
Function prototype : int fdopen(int handle,char* type);
Function : Connect stream with file handle
Parameters : int handle is the file handle to be operated, char *type specifies the stream opening method
Return value : Returns the file pointer pointing to the stream
Program example : Use a function to connect a file handle and a stream, and input a line of string into the file in the form of a stream.
#include<stdio.h>#include<io.h>#include<fcntl.h>intmain(void){intfd=open(D:\a.txt,_O_RDWR+O_CREAT);if(fd==-1 ){printf( cannotreatethefilen);return1;}FILE*fp=fdopen(fd,w);fprintf(fp,mingrisoftnminribookn);fclose(fp);printf(OK);return0;}
Running results
OK