Table of contents
read
man 2 read
parameter fd
file descriptor.
parameterbuf
buffer for storing data.
parameter count
buffer size.
return value
Greater than 0: the number of bytes read.
0: The end has been read, and the read end has been closed .
-1: Determine the errno value.
errno value:
EAGAIN or EWOULDBLOCK : Set the non-blocking mode to read, read a device file or network file. No data has arrived.
EINTR : A slow system call was interrupted.
Other : abnormal.
write
man 2 write
parameter fd
file descriptor.
parameterbuf
Buffer for data to be written out.
parameter count
data size.
return value
Success: The number of bytes written.
Failure: -1, errno is set.
test code
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
int main(int argc,char *argv[]){
char buf[1024];
int n=0;
int fp1=open(argv[1],O_RDONLY);
if(fp1==-1){
perror("fp1 error");
exit(1);
}
int fp2=open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0664);
if(fp2==-1){
perror("fp2 error");
exit(1);
}
while((n=read(fp1,buf,1024))!=0){
if(n<0){
perror("read error");
break;
}
write(fp2,buf,n);
}
close(fp1);
close(fp2);
return 0;
}