- read函数:
- 功能:从
文件fd中读取count个字节,存放进指针buf; - 具体内容:
#include
ssize_t read(int fd, void *buf, size_t count);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- write函数:
- 功能:把
指针buf中的内容,写count个字节到文件fd中; - 具体内容:
#include
ssize_t write(int fd, const void *buf, size_t count);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
#include
#include
#include
#include
int main(int argc, const char *argv[]){
if(3 != argc){
printf("Usage : %s src_file dest_file\n",argv[0]);
return -1;
}
int fd1 = open(argv[1],O_RDONLY);
if(-1 == fd1)
{
perror("open error");
return -1;
}
int fd2 = open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0666);
if(-1 == fd2)
{
perror("open error");
return -1;
}
int ret = 0;
char buff[128] = {0};
while(0 < (ret = read(fd1,buff,sizeof(buff)))){
write(fd2,buff,ret);
}
close(fd1);
close(fd2);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55