首页 > 系统相关 >Linux文件操作函数仿写cat命令,diff命令,cp命令

Linux文件操作函数仿写cat命令,diff命令,cp命令

时间:2024-08-11 16:59:04浏览次数:7  
标签:return 仿写 int cat fd1 命令 fd2 printf include

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc, const char *argv[])
{
	printf("参数个数=%d\n",argc);
	if(2 >argc)
	{
		printf("错误,至少需要1个参数\n");
		return -1;
	}

	for(int i=1;i<argc;i++)
	{
		int fd=open(argv[i],O_RDONLY);
		if(-1 == fd)
		{
			//printf("打开%S失败,错误为%S\n",argv[i],strerror(errno));
			perror("打开失败");
			return-1;
		}

		char buf[100]={0};
		while(1)
		{
			memset(buf,0,sizeof(buf));
			int ret=read(fd,buf,sizeof(buf)-1);
			if(0 == ret)
				break;
			printf("%s\n",buf);
		}
		close(fd);
	}


	putchar(10);
	return 0;
}
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int main(int argc, const char *argv[])
{
	if(3 != argc)
	{
		printf("filename error");
		return -1;
	}

	int fd1=open(argv[1],O_RDONLY);
	if(-1 == fd1)
	{
		perror("open fil1e error");
		return -1;
	}
	int fd2=open(argv[2],O_RDONLY);
	if(-1 == fd2)
	{
		perror("open fil2e error");
		return -1;
	}

	//比较文件大小
	int size1=lseek(fd1,0,SEEK_END);
	int size2=lseek(fd2,0,SEEK_END);

	lseek(fd1,0,SEEK_SET);
	lseek(fd2,0,SEEK_SET);

	char buf1[10]={0};
	char buf2[10]={0};

	if(size1 != size2)
	{
		printf("file different\n");
		return -1;
	}
	
	while(1)
	{
		memset(buf1,0,sizeof(buf1));
		int ret1=read(fd1,buf1,sizeof(buf1)-1);	
		memset(buf2,0,sizeof(buf2));
		int ret2=read(fd2,buf2,sizeof(buf2)-1);
		//比较大小
		if(0 != strcmp(buf1,buf2))
		{
			printf("different\n");
			return -1;
		}
		if(0 == ret2)
		{
			printf("file相同\n");
			break;
		}
	}
	
	
	return 0;

	close(fd1);
	close(fd2);
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

int main(int argc, char **argv)

{
	printf("argc=%d\n",argc);
	if(3 != argc)
	{
		printf("错误,ineed two file");
		return -1;
	}

	int fd1=open("./1.txt",O_RDWR);
	if(-1 == fd1)
	{
		perror("创建失败");
		return -1;
	
	}
	int fd2=open(argv[2],O_RDWR | O_CREAT | O_TRUNC,0644);
	if(-1 == fd2)
	{
		printf("创建%s失败,错误为%s\n",argv[2],strerror(errno));
		return -1;
	}
	
	while(1)
	{
		char buf[100]={0};
		memset(buf,0,sizeof(buf));
		int ret=read(fd1,buf,sizeof(buf)-1);
		write(fd2,buf,strlen(buf));
		if(0 == ret)
			break;
	}

	close(fd1);
	close(fd2);
	return 0;
}

标签:return,仿写,int,cat,fd1,命令,fd2,printf,include
From: https://blog.csdn.net/zxw190629/article/details/141044654

相关文章

  • 【转载】shell-命令行参数
    命令行参数(转自http://c.biancheng.net/cpp/view/2739.html)特殊变量列表变量含义$0当前脚本的文件名$n传递给脚本或函数的参数。n是一个数字,表示第几个参数。例如,第一个参数是$1,第二个参数是$2。$#传递给脚本或函数的参数个数。$*传递给脚本或函数的所有参数。......
  • 解决zabbix用户无法使用psql命令的问题
    解决zabbix用户无法使用psql命令的问题1.创建文件夹登录zabbix用户,创建所需的文件夹。su-zabbixcd/home/zabbixmkdirlibmkdirzabbix_envmkdirpg_binvitest.sh#创建测试脚本vitest.shcattest.shPGSHELL_CONFDIR="$1"source$PGSHELL_CONFDIR/zabbix_shell......
  • 解决yum命令无法使用的情况
    1.CentOSyum出现无法使用的情况1问题产生​ 新安装好的虚拟机无法使用yum命令来安装wget,一直提示报错。已加载插件:fastestmirrorCouldnotretrievemirrorlisthttp://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stockerrorwas14:curl#7-"Failedt......
  • 轻松搞定 RAR、Zip压缩包密码!Hashcat +john the ripper 亲测好用!
    不需要密码字典,可以用GPU来强行破解,速度非常快。GPU驱动程序要求:Linux上的AMDGPU需要“AMDGPU”(21.50或更高版本)和“ROCm”(5.0或更高版本)Windows上的AMDGPU需要“AMDAdrenalinEdition”(确切地说是Adrenalin22.5.1)IntelCPU需要“IntelCore和IntelXeon......
  • Android14音频进阶调试之命令播放mp3/aac非裸流音频(八十)
    简介:CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长!新书发布:《Android系统多媒体进阶实战》......
  • 开启/关闭子系统的命令
    WSL下的运行我们需要怎么关闭或者开启子系统呢用管理员权限打开的命令行先用wsl-l-v查看当前有无任务我这边是关闭的,于是我想把它开上,输入netstartLxssManager已经开启,接下来去想去的地方操作就好了如果要关掉子系统呢直接netstopLxssManager即可关闭......
  • iOS Object-C 创建类别(Category) 与使用
    有时候使用系统给出类或者第三方的类,但是呢它们自带的属性和方法又太少,不够我们的业务使用,这时候就需要给“系统的类或者第三方类”创建一个类别(Category),把自己的想添加的属性和方法写进来.Category模式用于向已经存在的类添加方法从而达到扩展已有类的目的一:创建Cate......
  • Linux常用命令(图文并茂+超详细!)
    ......
  • k8s命令
    k8s关键命令集群详细信息:显示Kubernetes版本:kubectlversion显示集群信息:kubectlcluster-info列出集群中的所有节点:kubectlgetnodes查看一个具体的节点详情:kubectldescribenodesrenode列出所有命名空间:kubectlgetnamespaces列出所有命名......
  • 把catoiOS介绍翻译10遍
    原文:catoiOS这是什么?catoiOS(catsOlympiadinInformaticsOpenSystem),是一个丰富多样的开放系统,其代码符合跨平台要求。它包含游戏、工具,还有个性化设置。包括各种活动在内,catoiOS致力于满足用户的所有需求.如何查看、下载catoiOScatoiOS官网下载catoiOS我可以一起开发......