linux(虚拟机、Ubuntu):编码实现把文件1(1.txt)复制到文件2(2.txt)

#include<sys/types.h>
#include<sys/stat.h>
#include <fcntl.h>
#include<unistd.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
	int a, b;
	char buff[64], buff1[58];
	int fd = open("1.txt", O_RDWR);	//打开1.txt	
	if(fd<0)
		printf("open 1.txt error\n");
	a = read(fd, buff, 64);//读取1.txt内容到buff				

	int fd1 = open("2.txt", O_RDWR);//打开2.txt	
	if(fd<0)
		printf("open 2.txt error\n");
	lseek(fd, 0, SEEK_SET);//定位到开头				
	b = write(fd1,buff,strlen(buff));//写入buff到fd1	

	close(fd);//关闭fd
	close(fd1);//关闭fd1
	return 0;
}

终端编译:gcc  文件名.c -o 文件名 

执行文件:./文件名

查询文件:cat 1.txt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值