c语言 编程实现 copy 命令。执行格式:copy file1 file2 file3

c语言 编程实现 copy 命令。执行格式:copy file1 file2 file3

方法1:

#include <stdio.h>
int main(){
	FILE *fp1=fopen("file1.txt", "r");
	FILE *fp2=fopen("file2.txt", "r");
	FILE *fp3=fopen("file3.txt", "w");
	char ch1;
	char ch2;
	while( (ch1=fgetc(fp1) )!= EOF )
		fputc(ch1,fp3);
    while( (ch2=fgetc(fp2) )!=EOF)	
    	fputc(ch2,fp3);
	
	printf("Merged file1.txt and file2.txt into file3.txt");
	
	fclose(fp1);
	fclose(fp2);
	fclose(fp3);
	return 0;
} 

方法2:

#include <sys/types.h>  
#include <sys/stat.h> 
#include <fcntl.h>
#include <stdlib.h>  
#include <errno.h> 
#include <stdio.h> 
#include <unistd.h> 
int main(int argc, char const *argv[])  
{  
int file1,file2,file3; 
	if (argc!= 4) {      
		printf("Usage: copy file1 file2 file3\n");   
		exit(1);  
	}   
	file1=open(argv[1],O_RDONLY);  
	file2=open(argv[2],O_RDONLY);  
	file3=open(argv[3],O_CREAT|O_RDWR,S_IRWXU);  
	int n;  
	char buf[1024];  
	while (( n=read(file1,buf,1024))>0) 
	if (write(file3,buf,n)!=n)  
		printf("write error\n");   
	if (n<0)  
		printf("read %s error\nErrno= %d\n",argv[1],errno);          
	if (lseek(file3,30,SEEK_END)==-1) 
	printf("lseek error ");  
	while (( n=read(file2,buf,1024))>0)
	if (write(file3,buf,n)!=n)  
		printf("write error\n");   
	if (n<0)  
		printf("read %s error\nErrno= %d\n",argv[2],errno);   
	exit(0);            
	close(file1);  
	close(file2);  
	close(file3);  
	printf("success\n");  
	return 0; 
}

方法3:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(){
	FILE *fp1=fopen("file1", "r");
	FILE *fp2=fopen("file2", "r");
	FILE *fp3=fopen("file3", "w");
	int fd=-1;
	fd=open("file3",O_CREAT|O_RDWR,S_IRWXU);
	if(fd==-1){
		printf("open error");
	}

	char ch1;
	char ch2;
	while( (ch1=fgetc(fp1) )!= EOF ){
		fputc(ch1,fp3);
	}
	printf("lseek kaishi\n");
	int a = -1;
	a=lseek(fd,16,SEEK_CUR);
	if(a==-1){
		printf("lseek error");
	}
	printf("lseek jieshu\n");

    	while( (ch2=fgetc(fp2) )!=EOF){	
    		fputc(ch2,fp3);
	}

	printf("Merged file1.txt and file2.txt into file3.txt\n");
	
	fclose(fp1);
	fclose(fp2);
	fclose(fp3);
	return 0;
} 

方法4:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(){
	char buf1[1024]={0};
	char buf2[1024]={0};
	int fd1=-1;
	int fd2=-1;
	int fd3=-1;
	
	fd1=open("file1.txt",O_RDONLY);
	fd1=read(fd1,buf1,sizeof(buf1));
	fd2=open("file2.txt",O_RDONLY);
	fd2=read(fd2,buf2,sizeof(buf2));
	fd3=open("file3.txt",O_CREAT|O_RDWR,S_IRWXU);
	if(fd3==-1||fd1==-1||fd2==-1){
		printf("open error");
	}

	if(fd1!=-1&&fd2!=-1){
		int w1=write(fd3,buf1,fd1);
		if (w1==-1)
			printf("write1 error\n");
		int l1=lseek(fd3,1024,SEEK_CUR);
		if (l1==-1)
			printf("lseek error\n");
		int w2=write(fd3,buf2,fd2);
		if (w2==-1)
			printf("write2 error\n");
	}
	
	

	printf("Copy successful\n");
	
	close(fd1);
	close(fd2);
	close(fd3);
	return 0;
} 


#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(){
	char buf1[1024]={0};
	char buf2[1024]={0};
	int fd1=-1;
	int fd2=-1;
	int fd3=-1;
	int fd4=-1;
	int fd5=-1;
	
	fd1=open("file1.txt",O_RDONLY);
	fd1=read(fd1,buf1,sizeof(buf1));
	fd2=open("file2.txt",O_RDONLY);
	fd2=read(fd2,buf2,sizeof(buf2));
	fd3=open("file3.txt",O_CREAT|O_RDWR,S_IRWXU);
	fd4=open("file4.txt",O_CREAT|O_RDWR,S_IRWXU);
	fd5=open("file5.txt",O_CREAT|O_RDWR,S_IRWXU);
	if(fd4==-1||fd5==-1||fd3==-1||fd1==-1||fd2==-1){
		printf("open error");
	}

	if(fd1!=-1&&fd2!=-1){
		int w1=write(fd3,buf1,fd1);
		if (w1==-1)
			printf("write1 error\n");
		int l1=lseek(fd3,30,SEEK_CUR);
		if (l1==-1)
			printf("lseek error\n");
		int w2=write(fd3,buf2,fd2);
		if (w2==-1)
			printf("write2 error\n");
	}
	
	printf(" 30Byte ok\n");
	if(fd1!=-1&&fd2!=-1){
		int w1=write(fd4,buf1,fd1);
		if (w1==-1)
			printf("write1 error\n");
		int l1=lseek(fd4,1048576,SEEK_CUR);
		if (l1==-1)
			printf("lseek error\n");
		int w2=write(fd4,buf2,fd2);
		if (w2==-1)
			printf("write2 error\n");
	}
	
	printf(" 1M ok\n");

	if(fd1!=-1&&fd2!=-1){
		int w1=write(fd5,buf1,fd1);
		if (w1==-1)
			printf("write1 error\n");
		int l1=lseek(fd5,10485760,SEEK_CUR);
		if (l1==-1)
			printf("lseek error\n");
		int w2=write(fd5,buf2,fd2);
		if (w2==-1)
			printf("write2 error\n");
	}
	
	printf(" 10M ok\n");
	
	close(fd1);
	close(fd2);
	close(fd3);
	close(fd4);
	close(fd5);
	return 0;
} 



  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值