Linux系统编程 - 24. 文件IO之 Open/Write

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
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页