当前位置: 首页 > news >正文

Linux下的文件IO之系统IO

1. 知识点

读入写出,切记以我们程序为中心向文件或者别的什么东西读入写出(输入流输出流)

人话就是

文件向我们程序就是读入

程序向文件或者别的什么就是写出

2. open打开文件

open.c

/*************************************************************************
    > File Name: open.c
    > Author: lsf
    > Mail: lsf_2012@163.com 
    > Created Time: 2023年10月20日 星期五 10时30分06秒
 ************************************************************************/

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

//使用open函数
int main()
{

    //打开文件---1.txt文件存在
    int fd1 = open("./1.txt",O_RDWR);

    //创建文件
    int fd2 = open("./2.txt",O_RDWR | O_CREAT,0777);

    printf("fd1 = %d   fd2 = %d\n",fd1,fd2);

    //关闭文件
    close(fd1);
    close(fd2);

    return 0;
}

3. read读入文件

read.c

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

//使用read函数
int main()
{

    //打开文件---1.txt文件存在
    int fd1 = open("./1.txt",O_RDWR);
    char buf[10]={0};

    //读文件
    while(1){
        
        memset(buf,0,sizeof(buf));//清空缓存区
        ssize_t ret = read(fd1,buf,sizeof(buf)-1);

        printf("%s",buf);

        if(ret==0){
            break;
        }

    }

    //关闭文件
    close(fd1);
    return 0;
}

3.2 实现cat函数

cat.c

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

//自己写一个cat函数

int main(int argc, char **argv)//cat.out   1.txt     2.txt  3.txt  4.txt
{                              //argv[0]  argv[1] argv[2]

	if(argc == 1)
	{
		printf("请给我至少一个文件的路径\n");
		return 0;
	}
	
	int i;
	for(i=1;i<argc;i++)
	{
		int fd1 = open(argv[i],O_RDWR);
		
		char buf[10] = {0};
	
		while(1)
		{
			memset(buf,0,sizeof(buf));//内存重置,清空bzero(buf,sizeof(buf));
			ssize_t n = read(fd1,buf,sizeof(buf)-1);
			
			printf("%s",buf);
			
			//退出条件
			if(n == 0)
			{
				close(fd1);
				break;
			}
			
			
		}
		
		
	}
}

4. write写出文件

write.c

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

//使用write函数实现文件的复制

int main()
{

    int fd1 = open("./1.txt",O_RDONLY);
    int fd2 = open("./2.txt",O_WRONLY|O_CREAT,0777);
    char buf[10];

    //文件的复制
    while(1){
        //内存清空
        memset(buf,0,sizeof(buf));
        //读文件
        ssize_t n = read(fd1,buf,sizeof(buf)-1);
        //写文件
        write(fd2,buf,strlen(buf));

        if(n==0){
            close(fd1);
            close(fd2);
            break;
        }

    }


    return 0;
}

5. lseek光标函数

lseek.c

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

//使用lseek函数

int main()
{

    int fd1 = open("./4.txt",O_RDWR|O_CREAT,0777);
    char buf[12] = "hello world";
    char buf2[12];

    //写文件
    write(fd1,buf,strlen(buf));
    lseek(fd1,0,SEEK_SET);//使光标复位到起始为止
    
    //读文件
    memset(buf2,0,sizeof(buf2));//清空缓存区
    read(fd1,buf2,sizeof(buf2));

    printf("%s\n",buf2);

    close(fd1);

    return 0;
}

6. perror错误打印函数

7. 练习

7.1 自我实现多个文件一起复制 copy函数

my_copy.c

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


//自我实现copy函数

int main(int argc, char **argv)
{

    if(argc == 1)
	{
		printf("请给我至少一个文件的路径\n");
		return 0;
	}

    int i=0;
    for(i=1;i<argc;i+=2){
        int fd1 = open(argv[i],O_RDONLY);
        int fd2 = open(argv[i+1],O_WRONLY|O_CREAT,0777);
        char buf[12];

        //文件的复制
        while(1){
            //内存清空
            memset(buf,0,sizeof(buf));
            //读文件
            ssize_t n = read(fd1,buf,sizeof(buf)-1);
            //写文件
            write(fd2,buf,strlen(buf));

            if(n==0){
                close(fd1);
                close(fd2);
                break;
            }

        }
    }

    
    return 0;
}



7.2 自我实现两个文件的比较diff函数

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

//自我实现diff函数

int main(int argc,char** argv)
{

    if(argc == 1)
	{
		printf("请给我至少一个文件的路径\n");
		return 0;
	}

    //打开两个文件
    int fd1 = open(argv[1],O_RDONLY);
    int fd2 = open(argv[2],O_RDONLY);
    char buf1[5] = {0};
    char buf2[5] = {0};

    int flag = 0;//两文件相同

    while(1){
        //读取文件
        memset(buf1,0,sizeof(buf1));
        memset(buf2,0,sizeof(buf2));

        ssize_t n1 = read(fd1,buf1,sizeof(buf1)-1);
        ssize_t n2 = read(fd2,buf2,sizeof(buf2)-1);

        //比较读取出来的buf
        if(strcmp(buf1,buf2)!=0){
            flag = 1;//不同

            break;
        }

        if(n1==0){
            close(fd1);
            break;
        }
        if(n2==0){
            close(fd2);
            break;
        }
    }

    //至此
    if(flag){
        printf("两个文件不同\n");
    }else{
        printf("两文件相同\n");
    }
 
    return 0;
}

相关文章:

  • eclipse中设置自动补齐代码
  • 第六章 数据库和缓存
  • 区间合并笔记
  • 万界星空科技智能工厂主要建设模式
  • js校验多个时间段的时间是否有交叉
  • OrangePi 5:ROS2 Humble中使用激光雷达
  • java:springboot3集成swagger(springdoc-openapi-starter-webmvc-ui)
  • WSL2 docker GUI 界面
  • Python与ArcGIS系列(十三)UpdateCursor方法
  • webWorker解决单线程中的一些小问题和性能优化
  • 【C++】string类的接口综合运用
  • 第四阶|自在行草 暄桐教室,林曦书法 从书法之美到生活之美
  • PHP TCP服务端监听端口接收客户端RFID网络读卡器上传的读卡数据
  • 关于前端的学习思考-父子盒子溢出问题
  • 视频字幕处理+AI绘画,Runway 全功能超详细使用教程(4)
  • Pandas进阶:文本处理
  • 王者荣耀java版
  • git rebase冲突说明(base\remote\local概念说明)
  • uni-app+vue3 封装全局函数(详细完整的方法)
  • SQL中left join、right join、inner join等的区别
  • 法治日报整版聚焦:儿童能否成为短视频主角?该如何监管?
  • 中国巴西民间推动建立经第三方验证的“森林友好型”牛肉供应链
  • 江西吉水通报一男子拒服兵役:不得考公,两年内经商、升学等受限
  • 教育部基础教育教指委:小学阶段禁止学生独自使用开放式内容生成功能
  • 5年建成强化城市核心功能新引擎,上海北外滩“风景文化都是顶流”
  • 长沙潮宗街内“金丝楠木老屋文旅博物馆”起火:明火已扑灭,无伤亡