使用open、write、read,实现copy文件。

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
//Creat a file;
//input a sentence;
//Creat other file;
//Copy frist file to second file;
int main(){
	//open a new file
	printf("please input a new file:\n");
	char new[111]={0};
	scanf("%s",new);
	int fd = open(new,O_WRONLY|O_CREAT,0666);
	if(fd <0 ){
		printf("open error|!\n");
		return -1;
	}
	printf("open success!\n");


	char text[11723]={0};
	printf("please input a sentence:\n");
	scanf("%s",text);
	//write 
	int ret = write(fd,text,sizeof(text));
	if(ret<0){
		printf("write error!");
		return -1;
	}
	printf("write success!\n");
	char new2[100]={0};
	fd = open(new,O_RDONLY);
	
	printf("please input other file\n");
        scanf("%s",new2);
	int ne = open(new2,O_WRONLY|O_CREAT,0666);
	char copy[1024]={0};
	int a=0;
while(1){
	bzero(copy,sizeof(copy));
	int aa = read(fd,copy,sizeof(copy));
	int back = write(ne,copy,sizeof(copy));
	if(aa == 0){
		break;
	}
}
	//close the new file
	close(fd);
	close(ne);
	return 0;
	
}


猜你喜欢

转载自blog.csdn.net/yizhixiaoma/article/details/80424463
今日推荐