book@100ask:~/linux/sys/20201201$ ls
open.c
book@100ask:~/linux/sys/20201201$ sudo gcc -o open open.c
book@100ask:~/linux/sys/20201201$ ./open
File open error
book@100ask:~/linux/sys/20201201$ ls
open open.c
book@100ask:~/linux/sys/20201201$ sudo ./open
File length = 692
List file content
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int srcid,dstid;
unsigned char buf[1024];
int readlen;
memset(buf,0x00,1024);
srcid = open("/home/book/linux/sys/20201201/open.c",O_RDONLY);
if(srcid<0)
{
printf("File open error \n");
return 0;
}
dstid = open("/home/book/linux/sys/20201201/open.c.bak",O_WRONLY|O_CREAT);
if(dstid<0)
{
printf("File open error \n");
return 0;
}
readlen = read(srcid,buf,1024);
printf("File length = %d\n
Linux系统编程 - 24. 文件IO之 Open/Write
最后发布:2020-12-01 06:04:19首次发布:2020-12-01 06:04:19