美文网首页C++
C语言文件操作

C语言文件操作

作者: 哇丶九夏 | 来源:发表于2018-11-08 21:48 被阅读91次

    一. 文件指针

    1. 在C语言中,规定文件的结构体为FILE,FILE实际上是_iobuf的别名。

    struct _iobuf {
            char *_ptr;
            int   _cnt;
            char *_base;
            int   _flag;
            int   _file;
            int   _charbuf;
            int   _bufsiz;
            char *_tmpfname;
            };
    typedef struct _iobuf FILE;
    

    2. 定义一个指向文件的指针。

    FILE *fp;  //fp指向一个文件
    

    二. 打开文件和关闭文件。

    1. fopen();打开文件

    • 函数原型: FILE * fopen(char * Filename,char *Mode);
    • 返回类型是一个FILE*指针
    • Filename 是需要打开的文件路径
    • Mode 是打开文件的方式 如下:
    标识符 作用
    r 只读
    w 只写
    a 追加
    r+ 读和写
    w+ 读和写
    a+ 读和写
    rb 只读(二进制)
    wb 只写(二进制)
    ab 追加(二进制)
    rb+ 读和写(二进制)
    wb+ 读和写(二进制)
    ab+ 读和写(二进制)
    • 打开文件时一般需要判断是否打开成功。
    #include <stdio.h>
    int main()
    {
        FILE* fp;
        if ((fp = fopen("test.txt", "a+")) == NULL)
        {
            printf("打开文件失败!\n");
        }
        return 0;
    }
    
    • 打开文件失败的原因一般有以下4个:
      a). 使用只读(r)的方式打开空的文件。
      b). 指定的盘符不存在或者指定的路径不存在。
      c). 文件名包含一些稀奇古怪的符号 比如123@.txt
      d). 有些文件被其他程序占用。

    2.fclose();关闭文件

    • 函数原型:int fclose(FILE *File);
        fclose(fp);
    
    • 文件打开使用完成之后,必须要关闭,防止数据丢失。

    三. 文件内部指针。

    当我们打开一个文件之后,在文件内部有一个位置指针,用来指向文件当前的读写位置。该指针在文件打开时总是指向第一个字节的。随着我们对文件进行读写的过程,这个内部位置指针会发生变化。

    1. fseek(); 文件内部指针偏移

    • 函数原型:int fseek(FILE * File, long Offset, int Origin);
    • File 即为文件指针。通常为fp。
    • Offset指的是偏移量,正数向右偏移,负数向做偏移,偏移量为常数,后面通常加上L。
    • Origin 是文件偏移的起始点。0表示文件头 1表示当前位置 2表示文件尾
        fseek(fp, 16L, 0);   //从文件头向右偏移16个字节
        fseek(fp, -16L, 1);  //从当前位置向左偏移16个字节
        fseek(fp, 16L, 2);   //从文件尾向左偏移16个字节
    

    2. rewind(); 重置文件内部指针为文件头。

    • 函数原型:void rewind(FILE *File);
        rewind(fp);   //重置文件内部指针为文件头
    

    四. 文件的读写。

    1. fputc(); 和fgetc();

    • 函数原型:
    int  fputc(int Ch,FILE *File);      //把字符Ch写入文件File
    int  fgetc(FILE * File);            //从文件当前位置读字符
    
    • 案例:
    #include <stdio.h>
    int main()
    {
        FILE* fp;  //定义一个文件指针
        if ((fp = fopen("test.txt", "w+")) == NULL)   //打开test.txt
        {
            printf("打开文件失败!\n");
        }
        char ch = 'j';  //定义一个字符变量ch,初始化为'j'
        char ch1;
        fputc(ch, fp);  //将ch写入文件  注意:这里的文件内部指针会变化。
        rewind(fp);     //将变化后的文件内部指针重置为文件头
        ch1 = fgetc(fp); //从文件中读出一个字符 存放到ch1中
        printf("%c\n", ch1);
    
        fclose(fp);  //关闭文件
        return 0;
    }
    

    2. fputs(); 和fgets();

    • 函数原型:
    int    fputs(const char* Str, FILE* File);          //把字符串Str写入文件File
    char*  fgets(char* Buf, int MaxCount, FILE* File);  //从文件File中获取MaxCount个字符存放到Buf指向的字符串中,结尾补'\0'。
    
    • 案例:
    #include <stdio.h>
    int main()
    {
        FILE* fp;  //定义一个文件指针
        if ((fp = fopen("test.txt", "w+")) == NULL)   //打开test.txt
        {
            printf("打开文件失败!\n");
        }
        char str[30] = "+qq:380672980";  //定义一个字符串str,初始化为"+qq:380672980"
        char str1[30];
        fputs(str, fp);  //将str写入文件  注意:这里的文件内部指针会变化。
        rewind(fp);     //将变化后的文件内部指针重置为文件头
        fgets(str1,30,fp); //从文件中获取30个字符存放到str1中,结尾补'\0'。
        printf("%s\n", str1);
    
        fclose(fp);  //关闭文件
        return 0;
    }
    

    3. fprintf(); 和fscanf();

    • 函数原型:
      int fprintf(FILE * File, const char * Format, ...);
      int fscanf(FILE * File, const char * _Format, ...);
    • 案例:
    #include <stdio.h>
    int main()
    {
        FILE *fp;
        char str[20] = "C++";
        char str1[20] = "Python";
        char buf[20];
        char buf1[20];
        char buf2[20];
        
        if ((fp = fopen("test.txt", "w+")) == NULL)
        {
            printf("打开文件失败!");
            return 0;
        }
        fprintf(fp, "%s Vs %s", str, str1);   //把str和str按照"%s Vs %s"的格式写入fp
        rewind(fp);                         //重置位置
        fscanf(fp, "%s %s %s", buf, buf1, buf2);      //读取字符串
        printf("%s\n%s\n%s\n", buf, buf1, buf2);  //打印
    
        fclose(fp);
        return 0;
    }
    

    4. fread(); 和fwrite();

    • 函数原型:
    //从字符串往文件写 Size大小,写Count次。
    size_t fwrite(const void * Str, size_t Size,size_t Count, FILE * File);
    //从文件中每次读取ElementSize个字符,一共读取Count次。
    size_t fread(const void * DstBuf, size_t ElementSize,size_t Count, FILE * File);
    
    • 案例:
    #include <stdio.h>
    int main()
    {
        FILE *fp;
        char str[100] = "Is everyone here taday";
        char str1[100];
        if ((fp = fopen("test.txt", "w+")) == NULL)
        {
            printf("打开文件失败!");
            return 0;
        }
        fwrite(str, 3, 8, fp);  
        rewind(fp);
        fread(str1, 1, 23, fp);
        printf("%s\n", str1);
        fclose(fp);
        return 0;
    }
    
    

    相关文章

      网友评论

        本文标题:C语言文件操作

        本文链接:https://www.haomeiwen.com/subject/emcgxqtx.html