Function name : write
Header file : <io.h>
Function prototype : int write(int handle,void *buf,int len);
Function : Get the pointer position of the open file
Parameter : int handle is the file handle to obtain the file pointer
void *buf is the content to be written
int len is the length of the file to be written
Return value : Returns the length of the actual file content written
Program example : Write a string to a file, read the string in the file, and output the result
#include<stdio.h>#include<io.h>#include<fcntl.h>#include<string.h>intmain(void){intfd=open(D:\a.txt,O_RDWR+O_CREAT); if(fd==-1){printf(cannotopenthefilen);return1;}charbuf[1024] ={Ilovewww.dotcpp.comverymuch!},buf2[1024]={ };intlen=write(fd,buf,strlen(buf));lseek(fd ,0,SEEK_SET);intlen2=read(fd,buf2,1024);printf(%snlen=%dn,buf2,len);close(fd);return0;}
Running results
Ilovewww.dotcpp.comverymuch!len=32