• linux read、write,fgetc、fputc等等函数效率比较


     一、相关函数

      文件IO            标准IO

      read()     fgets()  fgetc()  fread()

      write()    fputs()  fputc()  fwrite()

              行缓存   有缓存, 但   全缓存

                    不是行缓存      

    二、比较read()、write() 和 fgetc()、fputc()的效率

      ① read()、write()代码示例:

    /*
        实现cp命令:
            用文件IO中的read()、write()实现文件拷贝,
            并与fgetc、fputc做效率比较
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <string.h>
    int main(int argc, char *argv[])
    {
        int rd_fd, wr_fd;
        char buf[128]={0};
        int rd_ret = 0;    
    
        if(argc < 3)
        {
            printf("please input src file and des file!
    ");
            return -1;
        }
    
        //打开要拷贝的文件
        rd_fd = open(argv[1], O_RDONLY);
        if(rd_fd < 0)
        {
            printf("open src file %s failure
    ", argv[1]);
            return -2;
        }
        printf("open src file %s succ, rd_fd = %d
    ", argv[1], rd_fd);
    
        //打开目标文件,文件不存在则创建新的目标文件
        wr_fd = open(argv[2], O_WRONLY|O_CREAT, 0777);
        if(wr_fd < 0)
        {
            printf("open des file %s failure
    ", argv[2]);
            return -3;
        }    
        while(1)
        {    
            rd_ret = read(rd_fd, buf, 128);
            if(rd_ret < 128)//判断数据是否读取完毕
            {
                break;
            }
            write(wr_fd, buf, rd_ret);
            memset(buf, 0, 128);//清理缓存
        }
        write(wr_fd, buf, rd_ret);
    
        //关闭文件描述符
        close(wr_fd);
        close(rd_fd);
        return 0;
    }
    View Code

     

      ② fgetc()、fputc()代码示例   

    /*
        用fgetc、fputc实现文件拷贝
    */
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        FILE *src_fp, *des_fp;
        int read_ret;
        if(argc < 3)
        {
            printf("please input src and des file
    ");
            return -1;
        }
        
        //打开原文件
        src_fp = fopen(argv[1],"r");
        if(src_fp == NULL)
        {
            printf("open src %s file failed!
    ", argv[1]);
            return -2;
        }
        
        //打开目标文件
        des_fp = fopen(argv[2], "w");
        if(des_fp == NULL)
        {
            printf("open des %s file failed!
    ",argv[2]);
            return -3;
        }
        
        while(1)
        {
            read_ret = fgetc(src_fp);//读取原文件
            if(feof(src_fp))//判断文件是否读取完毕
            {
                printf("read file %s endl!
    ", argv[1]);
                break;
            }    
            fputc(read_ret, des_fp);//写入原文件
        }
        
        fclose(des_fp);
        fclose(src_fp);
        return 0;
    }
    View Code

     三、结果展示

        

    四、fgets()、fread()函数比较

      ① 用fgets、fputs代码示例:

    /*
        用fgets、fputs实现文件拷贝
    */
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        FILE *src_fp, *des_fp;
        char buf[128]={0};
        if(argc < 3)
        {
            printf("please input src and des file
    ");
            return -1;
        }
        
        //打开原文件
        src_fp = fopen(argv[1],"r");
        if(src_fp == NULL)
        {
            printf("open src %s file failed!
    ", argv[1]);
            return -2;
        }
        
        //打开目标文件
        des_fp = fopen(argv[2], "w");
        if(des_fp == NULL)
        {
            printf("open des %s file failed!
    ",argv[2]);
            return -3;
        }
        
        while(1)
        {
            fgets(buf, 128, src_fp);
            if(feof(src_fp))
            {
                printf("read file %s endl!
    ",argv[1]);
                break;
            }
            fputs(buf, des_fp);
        }
        
        fclose(des_fp);
        fclose(src_fp);
        return 0;
    }
    View Code

      ② 用fread()、fwrite()代码示例:

    /*
        用fread、fwrite实现文件拷贝
    */
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        FILE *src_fp, *des_fp;
        char buf[128]={0};
        int read_ret;
        if(argc < 3)
        {
            printf("please input src and des file
    ");
            return -1;
        }
        
        //打开原文件
        src_fp = fopen(argv[1],"r");
        if(src_fp == NULL)
        {
            printf("open src %s file failed!
    ", argv[1]);
            return -2;
        }
        
        //打开目标文件
        des_fp = fopen(argv[2], "w");
        if(des_fp == NULL)
        {
            printf("open des %s file failed!
    ",argv[2]);
            return -3;
        }
        
        while(1)
        {
            read_ret = fread(buf, 1, 128, src_fp);
            fwrite(buf, 1, read_ret, des_fp);
            if(read_ret < 128)
            {
                printf("read file %s endl!
    ",argv[1]);
                break;
            }
        }
        
        fclose(des_fp);
        fclose(src_fp);
        return 0;
    }
    View Code

        

  • 相关阅读:
    Java调用CMD命令
    java在线截图---通过指定的URL对网站截图
    【好的技术员网站】蓝飞技术部落格==蓝飞技术部落格==蓝飞技术部落格
    事务管理最佳实践全面解析
    Javascript摸拟自由落体与上抛运动 说明!
    dwz中给表单项获取,设置值
    jquery操作select(取值,设置选中)[转]
    Flex 笔记整理 三
    Myeclipse 工具优化 [内存一直增加, jsp,javascript 编辑很卡]
    Spring: 读取 .properties 文件地址,json转java对象,el使用java类方法相关 (十三)
  • 原文地址:https://www.cnblogs.com/jiangson/p/6073061.html
Copyright © 2020-2023  润新知