zoukankan      html  css  js  c++  java
  • Linux 文件管理(C语言库函数一)

            系统调用函数能够直接操作系统设备,C语言库函数是对系统调用函数的封装,增加了可移植性,
    C语言库函数可以在各个系统上运行,而系统调用则会因为系统不同而有一定的差别。 在读写文件这个操作上,系统函数每次都会调用系统设备进行读写,但是C语言库函数则做了一定的优化,
    C语言库函数提供了一个缓冲区,只有当缓冲区满了才会调用系统设备读写文件,保护了磁盘,减少系统消耗。 但是在读取键盘文件的方法上,系统调用使用的是read()函数,而C语言可以函数使用的是scanf(),fscanf()函数,
    C语言的scanf()函数存在弊端(无法读取空格,不能按缓冲区大小读取),所以推荐使用系统调用。
    打开和关闭文件
    FILE * fopen(const char *path,const char *mode);
    int fclose(FILE *stream)
    fopen以mode模式打开名为path的文件
    fopen返回一个文件指针
    出现错误,fopen返回NULL,并把errno设置为恰当的值
    
    mode模式说明
    "r":以读的方式打开文件,文件流的位置在文件的开始。
    "r+":以读写的方式打开文件,文件流的位置在文件的开始。
    "w":如果文件存在,截断这个文件,让文件的大小变为0,文件流的位置在文件的开始。
    "w+":以读写的方式打开文件,如果文件不存在,建立该文件,如果文件存在就截断文件,文件流的位置在文件的开始。
    "a":以追加的方式打开文件,在文件的末尾开始写,如果文件不存在,创建该文件,文件流的位置在文件的末尾。
    "a+":以读写的方式打开文件(如果是写文件,在文件的末尾开始写),如果文件不存在,创建该文件;从文件头开始读文件,但是在文件尾部追加文件。
    备注:在Linux系统环境下,读写二进制文件或者文本文件只有这6种mode模式,
    但是在windows系统环境下,读写文本文件是这6种模式,读写二进制文件则必须要加上'b',例如"rb"--读二进制文件
    size_t fread(void *ptr,size_t size,size_t nmemb,FILE *stream)
    size_t fwrite(void *ptr,size_t size,size_t nmemb,FILE *stream)
    参数ptr指向缓冲区保存或读取的数据
    参数size表示单个记录大小
    参数nmemb最大允许读取或回写的记录数
    函数返回值是已经读取或回写的记录数
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    typedef struct _person
    {
        int num;
        char name[30];
        int age;
    } Person;
    
    int main(int arg, char * args[])
    {
        if (arg < 2)
        {
            printf("请输入一个参数!
    ");
            return 0;
        }
        //open the file
        FILE * pf = NULL;
        //open the file in write mode
        pf = fopen(args[1], "w");
        //judge file pointer
        if (pf == NULL)
        {
            /*
             注意:这里和系统函数read有所区别,系统read函数返回的是int型文件标识符
             而C语言库函数返回的是FILE结构体指针
             系统read函数判断文件标识符是否是-1
             C语言库函数fread判断返回的文件指针是否为NULL
             */
            printf("error msg:%s
    ", strerror(errno));
            return 0;
        }
        /*
         fread()和fwrite()一般用于二进制文件的读写
         */
    
        //size_t类型相当于unsigned int(无符号整型)
        size_t rc=0;
        //define person object
        //Initialization
        Person arr[3];
        memset(&arr, 0, sizeof(arr));
        arr[0].num = 1;
        strcpy(arr[0].name, "ming");
        arr[0].age = 12;
    
        arr[1].num = 2;
        strcpy(arr[1].name, "hong");
        arr[1].age = 13;
    
        arr[2].num = 3;
        strcpy(arr[2].name, "fei");
        arr[2].age = 14;
    
        rc= fwrite(arr,sizeof(Person),3,pf);
        //print record count
        printf("record count:%d
    ",rc);
        //close file pointer
        fclose(pf);
        return 0;
    }
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    typedef struct _person
    {
        int num;
        char name[30];
        int age;
    } Person;
    
    int main(int arg, char * args[])
    {
        if (arg < 2)
        {
            printf("请输入一个参数!
    ");
            return 0;
        }
        //open the file
        FILE * pf = NULL;
        //open the file in read mode
        pf = fopen(args[1], "r");
        //judge file pointer
        if (pf == NULL)
        {
    
            printf("error msg:%s
    ", strerror(errno));
            return 0;
        }
        size_t rc=0;
        int i=0;
        Person arr[3];
        memset(&arr,0,sizeof(arr));
        rc= fread(arr,sizeof(Person),3,pf);
        //print record count
        printf("record count:%d
    ",rc);
        for(;i<3;i++)
        {
            printf("num=%d
    ",arr[i].num);
            printf("name=%s
    ",arr[i].name);
            printf("age=%d
    ",arr[i].age);
        }
        //close file pointer
        fclose(pf);
        return 0;
    }
    格式化输入和输出调用
    int fprintf(FILE * stream,const char *format,...)
    int fscanf(FILE * stream,const char *format,...)
    //fprintf()函数
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    int main(int arg, char * args[])
    {
        if (arg < 2)
        {
            printf("请输入一个参数!
    ");
            return 0;
        }
        //open the file
        FILE * pf = NULL;
        //open the file in read mode
        pf = fopen(args[1], "w");
        //judge file pointer
        if (pf == NULL)
        {
    
            printf("error msg:%s
    ", strerror(errno));
            return 0;
        }
        char buf[30]={0};
        int i=0;
        strcpy(buf,"fly on air");
        for(;i<10;i++)
        {
            fprintf(pf,"%s--num=%d
    ",buf,i);
        }
        //close file pointer
        fclose(pf);
        return 0;
    }
    //fscanf()函数
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    int main(int arg, char * args[])
    {
        if (arg < 2)
        {
            printf("请输入一个参数!
    ");
            return 0;
        }
        //open the file
        FILE * pf = NULL;
        //open the file in read mode
        pf = fopen(args[1], "r");
        //judge file pointer
        if (pf == NULL)
        {
    
            printf("error msg:%s
    ", strerror(errno));
            return 0;
        }
        char buf[10]={0};
        //EOF是一个常量,表示文件结尾
        /*
         scanf(),fscanf()两大缺点
         缺点一:遇到空格或者'
    '结束当前读取
         缺点二:不会考虑缓冲区的大小,读取的内容很可能超出缓冲区大小,导致报错
         */
        while(fscanf(pf,"%s",buf)!=EOF)
        {
            printf("%s
    ",buf);
        }
        //close file pointer
        fclose(pf);
        return 0;
    }
    行输入和输出调用。
    char *fgets(char *s, int size, FILE *stream);
    int fputc(char *s, FILE *stream);
    fgets从文件中读取一行,返回EOF代表文件结尾。
    fputs向文件中写入一行
    //fputs()函数
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    int main(int arg, char * args[])
    {
        if (arg < 2)
        {
            printf("请输入一个参数!
    ");
            return 0;
        }
        //open the file
        FILE * pf = NULL;
        //open the file in write mode
        pf = fopen(args[1], "w");
        //judge file pointer
        if (pf == NULL)
        {
    
            printf("error msg:%s
    ", strerror(errno));
            return 0;
        }
        char buf[50]={0};
        sprintf(buf,"fly with me
    ");
        fputs(buf,pf);
        //close file pointer
        fclose(pf);
        return 0;
    }
    //fgets()函数
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    int main(int arg, char * args[])
    {
        if (arg < 2)
        {
            printf("请输入一个参数!
    ");
            return 0;
        }
        //open the file
        FILE * pf = NULL;
        //open the file in read mode
        pf = fopen(args[1], "r");
        //judge file pointer
        if (pf == NULL)
        {
    
            printf("error msg:%s
    ", strerror(errno));
            return 0;
        }
        char buf[50]={0};
        /*
         fgets()函数读到文件结尾会返回NULL,不是EOF
    
         */
        while(fgets(buf,sizeof(buf),pf)!=NULL)
        {
            /*
             fgets()会读出每一行的换行符'
    ',所以读出来的数据不要加'
    '
             fscanf()不会读出'
    '
             */
            printf("%s",buf);
        }
    
        //close file pointer
        fclose(pf);
        return 0;
    }
  • 相关阅读:
    【TS】535- 7个超好用的 TypeScript 新功能
    【学习】一起加入重学 TypeScript 学习小组
    17.5W秒级交易峰值下的混合云弹性架构之路
    微服务架构:spring cloud之服务注册和服务发现
    消息队列服务RabbitMQ 和Kafka对比
    微服务架构:spring cloud简介
    2016 年度码云热门项目排行榜 TOP 10
    Netflix Conductor : 一个微服务的编排器
    Java 9的这一基本功能,你可能从未听过
    使用 Docker 搭建 Java Web 运行环境
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/5793300.html
Copyright © 2011-2022 走看看