0
点赞
收藏
分享

微信扫一扫

使用Gitlab构建简单流水线CI/CD

邯唐情感 2023-10-22 阅读 22

本章重点:

 1.  什么是文件

 1.1 程序文件

 1.2 数据文件

 1.3 文件名

 2. 文件的打开和关闭

 2.1 文件指针

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

不同的C编译器的FILE类型包含的内容不完全相同,但是大同小异。

每当打开一个文件的时候,系统会根据文件的情况自动创建一个FILE结构的变量,并填充其中的信息,使用者不必关

心细节。

一般都是通过一个FILE的指针来维护这个FILE结构的变量,这样使用起来更加方便。

下面我们可以创建一个FILE*的指针变量:

定义pf是一个指向FILE类型数据的指针变量。可以使pf指向某个文件的文件信息区(是一个结构体变量)。通过该文 件信息区中的信息就能够访问该文件。也就是说,通过文件指针变量能够找到与它关联的文件。 

2.2 文件的打开与关闭

int main()
{
    //相对路径
    //绝对路径
    ///Users/fan/Documents/c_study/c_test26/data.txt

    //打开文件
    FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");
    if (pf == NULL)
    {
        perror("fopen");
        return 1;
    }

    //读文件


    //关闭文件
    fclose(pf);
    pf = NULL;


    return 0;
}

 3. 文件的顺序读写

 3.1 顺序读写函数介绍

 

 

int main()
{
    //相对路径
    //绝对路径
    ///Users/fan/Documents/c_study/c_test26/data.txt

    //打开文件
    FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","w");
    if (pf == NULL)
    {
        perror("fopen");
        return 1;
    }

    //读文件
    //写文件
    // fputc('a',pf);
    // fputc('b',pf);
    // fputc('c',pf);
    int i = 0;
    for (i = 0; i < 26; i++)
    {
        fputc('a'+i,pf);
    }


    //关闭文件
    fclose(pf);
    pf = NULL;


    return 0;
}
int main()
{
    //相对路径
    //绝对路径
    ///Users/fan/Documents/c_study/c_test26/data.txt

    //打开文件
    FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");
    if (pf == NULL)
    {
        perror("fopen");
        return 1;
    }

    //读文件
    int ch = fgetc(pf);
    printf("%c\n",ch);  //a
    ch = fgetc(pf);
    printf("%c\n",ch); //b
    ch = fgetc(pf);
    printf("%c\n",ch); //c
    ch = fgetc(pf);
    printf("%c\n",ch);//d


    //写文件
    // fputc('a',pf);
    // fputc('b',pf);
    // fputc('c',pf);
    // int i = 0;
    // for (i = 0; i < 26; i++)
    // {
    //     fputc('a'+i,pf);
    // }


    //关闭文件
    fclose(pf);
    pf = NULL;


    return 0;
}
int main()
{
    //相对路径
    //绝对路径
    ///Users/fan/Documents/c_study/c_test26/data.txt

    //打开文件
    FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");
    if (pf == NULL)
    {
        perror("fopen");
        return 1;
    }

    //读文件
    // int ch = fgetc(pf);
    // printf("%c\n",ch);  //a
    // ch = fgetc(pf);
    // printf("%c\n",ch); //b
    // ch = fgetc(pf);
    // printf("%c\n",ch); //c
    // ch = fgetc(pf);
    // printf("%c\n",ch);//d


    //写文件
    // fputc('a',pf);
    // fputc('b',pf);
    // fputc('c',pf);
    // int i = 0;
    // for (i = 0; i < 26; i++)
    // {
    //     fputc('a'+i,pf);
    // }

    // fputs("hello fan\n",pf);
    // fputs("hello fanfan\n",pf);

    //读文件 - 读一行
    char arr[10] = {0};
    fgets(arr,15,pf);
    printf("%s\n",arr);
    fgets(arr,15,pf);
    printf("%s\n",arr);


    //关闭文件
    fclose(pf);
    pf = NULL;


    return 0;
}
struct S
{
    int a;
    float s;
};

int main()
{
    //相对路径
    //绝对路径
    ///Users/fan/Documents/c_study/c_test26/data.txt

    //打开文件
    FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");
    if (pf == NULL)
    {
        perror("fopen");
        return 1;
    }

    //写文件
    //struct S s = {100,3.14f};
    //fprintf(pf,"%d %f",s.a,s.s);
    struct S s = {0};
    fscanf(pf,"%d %f",&(s.a),&(s.s));

    printf("%d %f",s.a,s.s);


    //关闭文件
    fclose(pf);
    pf = NULL;


    return 0;
}
struct S
{
    int a;
    float s;
    char str[10];
};

int main()
{

    struct S s = {99, 6.18f, "bit"};
    //打开文件
    FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","wb");
    if (pf == NULL)
    {
        perror("fopen");
        return 1;
    }
    //写文件
    fwrite(&s,sizeof(struct S),1,pf);

    //关闭文件
    fclose(pf);
    pf = NULL;

    return 0;
}
int main()
{

    struct S s = {0};
    //打开文件
    FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","rb");
    if (pf == NULL)
    {
        perror("fopen");
        return 1;
    }
    
    //读文件
    fread(&s,sizeof(struct S),1,pf);
    printf("%d %f %s\n",s.a,s.s,s.str);

    //关闭文件
    fclose(pf);
    pf = NULL;

    return 0;
}

 4. 文件的随机读写

 4.1 fseek

int main()
{
    FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");
    if (pf == NULL)
    {
        perror("fopen");
        return 1;
    }

    //读文件
    //定位文件指针到f
    int ch = fgetc(pf);
    printf("%c\n",ch);   //a

    ch = fgetc(pf);
    printf("%c\n",ch);  //b

    ch = fgetc(pf);
    printf("%c\n",ch);  //c 

    ch = fgetc(pf);
    printf("%c\n",ch);  //d

    fseek(pf,-3,SEEK_END);
    ch = fgetc(pf);
    printf("%c\n",ch);


    //关闭文件
    fclose(pf);
    pf = NULL;



    return 0;
}

4.2 ftell

int main()
{
    FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");
    if (pf == NULL)
    {
        perror("fopen");
        return 1;
    }

    //读文件
    //定位文件指针到f
    int ch = fgetc(pf);
    printf("%c\n",ch);   //a

    ch = fgetc(pf);
    printf("%c\n",ch);  //b

    ch = fgetc(pf);
    printf("%c\n",ch);  //c 

    ch = fgetc(pf);
    printf("%c\n",ch);  //d

    // fseek(pf,-3,SEEK_END);
    // ch = fgetc(pf);
    // printf("%c\n",ch);
    int pos = ftell(pf);
    printf("%d\n",pos); //4


    //关闭文件
    fclose(pf);
    pf = NULL;



    return 0;
}

4.3 rewind 

int main()
{
    FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");
    if (pf == NULL)
    {
        perror("fopen");
        return 1;
    }

    //读文件
    //定位文件指针到f
    int ch = fgetc(pf);
    printf("%c\n",ch);   //a

    ch = fgetc(pf);
    printf("%c\n",ch);  //b

    ch = fgetc(pf);
    printf("%c\n",ch);  //c 

    ch = fgetc(pf);
    printf("%c\n",ch);  //d

    // fseek(pf,-3,SEEK_END);
    // ch = fgetc(pf);
    // printf("%c\n",ch);
    // int pos = ftell(pf);
    // printf("%d\n",pos); //4

    rewind(pf);  //回到起始位置

    ch = fgetc(pf);
    printf("%c\n",ch); //a

    //关闭文件
    fclose(pf);
    pf = NULL;

    return 0;
}

 5. 文本文件和二进制文件

6. 文件读取结束的判断  

6.1 被错误使用的feof

int main(void)
{
    int c; // 注意:int,非char,要求处理EOF
    FILE *fp = fopen("test.txt", "r");
    if (!fp)
    {
        perror("File opening failed");
        return EXIT_FAILURE;
    }
    // fgetc 当读取失败的时候或者遇到文件结束的时候,都会返回EOF
    while ((c = fgetc(fp)) != EOF) // 标准C I/O读取文件循环
    {
        putchar(c);
    }
    // 判断是什么原因结束的
    if (ferror(fp))
        puts("I/O error when reading");
    else if (feof(fp))
        puts("End of file reached successfully");
    fclose(fp);
}

//拷贝文件
//拷贝data.txt文件,产生一个新文件data1.txt

int main()
{
    FILE* pfRead = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");
    if (pfRead == NULL)
    {
        perror("open file for read");
        return 1;

    }

    FILE* pfWrite = fopen("/Users/fan/Documents/c_study/c_test26/data2.txt","w");
    if (pfWrite == NULL)
    {
        perror("open file for write");
        fclose(pfRead);
        pfRead = NULL;
        return 1;
    }


    //读写文件
    int ch = 0;
    while((ch = fgetc(pfRead)) != EOF)
    {
        fputc(ch,pfWrite);
    }


    //关闭文件
    fclose(pfRead);
    pfRead = NULL;

    fclose(pfWrite);
    pfWrite = NULL;


    return 0;
}

 7. 文件缓冲区

举报

相关推荐

0 条评论