文件IO操作

一、打开文件

cpp 复制代码
#include<stdio.h>
int main()
{
    /*
    const char *__restrict__ __filename 要打开的文件名
    const char *__restrict__ __modes 要打开文件的访问模式
    【1】r:只读模式
    【2】w:只写模式,如果文件存在清空文件 如果文件不存在创建文件
    【3】a:只追加写模式,如果文件存在末尾追加写 如果不存在创建新文件
    【4】r+:读写模式 文件必须存在 写入是从头一个一个覆盖
    【5】w+:读写模式 如果文件存在清空文件 如果不存在创建新文件
    【6】a+:读追加写模式 如果文件存在末尾追加写 如果不存在创建文件
    return :FILE* 结构体指针 表示一个文件 
            报错返回NULL
    FILE *fopen(const char *__restrict__ __filename, const char *__restrict__ __modes)
    
    */
    
    char *filename="io.txt";
    char *modes="w";
    FILE *ioFile = fopen(filename,modes);
    if (ioFile == NULL)
    {
        printf("打开失败\n");
    }
    else
    {
        printf("打开成功\n");
    }
    
    return 0;
}

MakeFile文件

cpp 复制代码
CC:=gcc
fopen_test:fopen_test.c
#$(CC) -o $@ $^ 表示gcc -o fopen_test fopen_test.c
	-$(CC) -o $@ $^ 
#./$@ 表示./fopen_test
	-./$@
#rm ./$@ 表示rm ./fopen_test
	-rm ./$@

二、关闭文件

cpp 复制代码
#include<stdio.h>
int main()
{
    /*
    const char *__restrict__ __filename 要打开的文件名
    const char *__restrict__ __modes 要打开文件的访问模式
    【1】r:只读模式
    【2】w:只写模式,如果文件存在清空文件 如果文件不存在创建文件
    【3】a:只追加写模式,如果文件存在末尾追加写 如果不存在创建新文件
    【4】r+:读写模式 文件必须存在 写入是从头一个一个覆盖
    【5】w+:读写模式 如果文件存在清空文件 如果不存在创建新文件
    【6】a+:读追加写模式 如果文件存在末尾追加写 如果不存在创建文件
    return :FILE* 结构体指针 表示一个文件 
            报错返回NULL
    FILE *fopen(const char *__restrict__ __filename, const char *__restrict__ __modes)
    
    */
    
    char *filename="io.txt";
    char *modes="w";
    FILE *ioFile = fopen(filename,modes);
    if (ioFile == NULL)
    {
        printf("打开失败\n");
    }
    else
    {
        printf("打开成功\n");
    }
    
    /*
    FILE *__stream 需要关闭的文件
    return: 成功返回0 失败返回EOF(负数)通常关闭文件失败会直接报错
    int fclose(FILE *__stream)
    */

    int result=fclose(ioFile);
    if (result == 0)
    {
        printf("关闭文件成功\n");
    }
    else if(result == EOF)
    {
        printf("关闭文件失败\n");
    }
    

    return 0;
}

MakeFile文件

cpp 复制代码
fclose_test:fclose_test.c
	-$(CC) -o $@ $^ 
	-./$@
	-rm ./$@

三、写入字节fputc

cpp 复制代码
#include<stdio.h>
int main()
{
    /*
    const char *__restrict__ __filename 要打开的文件名
    const char *__restrict__ __modes 要打开文件的访问模式
    【1】r:只读模式
    【2】w:只写模式,如果文件存在清空文件 如果文件不存在创建文件
    【3】a:只追加写模式,如果文件存在末尾追加写 如果不存在创建新文件
    【4】r+:读写模式 文件必须存在 写入是从头一个一个覆盖
    【5】w+:读写模式 如果文件存在清空文件 如果不存在创建新文件
    【6】a+:读追加写模式 如果文件存在末尾追加写 如果不存在创建文件
    return :FILE* 结构体指针 表示一个文件 
            报错返回NULL
    FILE *fopen(const char *__restrict__ __filename, const char *__restrict__ __modes)
    
    */
    
    char *filename="io.txt";
    char *modes="w";
    FILE *ioFile = fopen(filename,modes);
    if (ioFile == NULL)
    {
        printf("打开失败\n");
    }
    else
    {
        printf("打开成功\n");
    }
    
    /*
    int __c ASCII码对应的char
    FILE *__stream 打开的一个文件
    return:成功返回的char 失败返回EOF
    int fputc(int __c, FILE *__stream)
    */

    int put_result=fputc(97,ioFile);
    if (put_result==EOF)
    {
        printf("写入文件失败\n");
    }
    else
    {
        printf("写入文件成功\n");
    }
    

    /*
    FILE *__stream 需要关闭的文件
    return: 成功返回0 失败返回EOF(负数)通常关闭文件失败会直接报错
    int fclose(FILE *__stream)
    */

    int result=fclose(ioFile);
    if (result == 0)
    {
        printf("关闭文件成功\n");
    }
    else if(result == EOF)
    {
        printf("关闭文件失败\n");
    }
    

    return 0;
}

MakeFile文件

cpp 复制代码
fputc_test:fputc_test.c
	-$(CC) -o $@ $^ 
	-./$@
	-rm ./$@

四、写入字符串fputs

cpp 复制代码
#include<stdio.h>
int main()
{
    /*
    const char *__restrict__ __filename 要打开的文件名
    const char *__restrict__ __modes 要打开文件的访问模式
    【1】r:只读模式
    【2】w:只写模式,如果文件存在清空文件 如果文件不存在创建文件
    【3】a:只追加写模式,如果文件存在末尾追加写 如果不存在创建新文件
    【4】r+:读写模式 文件必须存在 写入是从头一个一个覆盖
    【5】w+:读写模式 如果文件存在清空文件 如果不存在创建新文件
    【6】a+:读追加写模式 如果文件存在末尾追加写 如果不存在创建文件
    return :FILE* 结构体指针 表示一个文件 
            报错返回NULL
    FILE *fopen(const char *__restrict__ __filename, const char *__restrict__ __modes)
    
    */
    
    char *filename="io.txt";
    char *modes="w";
    FILE *ioFile = fopen(filename,modes);
    if (ioFile == NULL)
    {
        printf("打开失败\n");
    }
    else
    {
        printf("打开成功\n");
    }
    
    /*
    const char *__restrict__ __s  需要写入的字符串
    FILE *__restrict__ __stream   需要写入的文件
    return :成功返回非负整数(0,1) 失败返回EOF
    int fputs(const char *__restrict__ __s, FILE *__restrict__ __stream)
    */

    int putsR=fputs("love letter\n",ioFile);
    if (putsR == EOF)
    {
        printf("写入字符串失败\n");
    }
    else
    {
        printf("写入字符串%d成功\n",putsR);
    }


    /*
    FILE *__stream 需要关闭的文件
    return: 成功返回0 失败返回EOF(负数)通常关闭文件失败会直接报错
    int fclose(FILE *__stream)
    */

    int result=fclose(ioFile);
    if (result == 0)
    {
        printf("关闭文件成功\n");
    }
    else if(result == EOF)
    {
        printf("关闭文件失败\n");
    }
    

    return 0;
}

MakeFile文件

cpp 复制代码
fputs_test:fputs_test.c
	-$(CC) -o $@ $^ 
	-./$@
	-rm ./$@

五、读取字节fgets

cpp 复制代码
#include<stdio.h>
int main()
{
    //打开文件
    FILE* ioFile = fopen("io.txt","r");
    if (ioFile == NULL)
    {
        printf("不能用只读模式打开不存在文件\n");
    }
    
    //读取文件内容
    /*
    FILE *__stream 打开的文件
    return:读取到的一个字节 如果出现错误或的到文件的末尾返回EOF
    int fgetc(FILE *__stream)
    */
   char c = fgetc(ioFile);

   while(c != EOF)
   {
    printf("%c",c);
    c= fgetc(ioFile);
   }

   printf("\n");
    
    //关闭文件
    int closeR = fclose(ioFile);
    if (closeR == EOF)
    {
        printf("关闭文件失败");
    }
    

    return 0;
}

MakeFile文件

cpp 复制代码
fgetc_test:fgetc_test.c
	-$(CC) -o $@ $^ 
	-./$@
	-rm ./$@

六、读取字符串fgets

cpp 复制代码
#include<stdio.h>
int main()
{
    //打开文件
    FILE* ioFile = fopen("io.txt","r");
    if (ioFile == NULL)
    {
        printf("不能用只读模式打开不存在文件\n");
    }
    
    //读取文件内容
    /*
    char *__restrict__ __s 接收读取到的字符串
    int __n 接收数据的长度
    FILE *__restrict__ __stream 打开要读取的文件
    retrun 成功返回字符串 失败返回NULL 
    char *fgets(char *__restrict__ __s, int __n, FILE *__restrict__ __stream)
    */
   char buffer[100];
   while (fgets(buffer,sizeof(buffer),ioFile))
   {
    printf("%s",buffer);
   }

   printf("\n");
    
    //关闭文件
    int closeR = fclose(ioFile);
    if (closeR == EOF)
    {
        printf("关闭文件失败");
    }
    

    return 0;
}

MakeFile文件

cpp 复制代码
fgets_test:fgets_test.c
	-$(CC) -o $@ $^ 
	-./$@
	-rm ./$@

七、格式化读取fscanf

cpp 复制代码
#include<stdio.h>
int main()
{
    //打开文件
    FILE* ioFile = fopen("io.txt","r");
    if (ioFile == NULL)
    {
        printf("不能用只读模式打开不存在文件\n");
    }
    
    //读取文件内容
    /*
    FILE *__restrict__ __stream 打开的文件
    const char *__restrict__ __format 带有格式化的字符串(固定格式接收)
    ...可变参数:填写格式化的字符串(接收数据前声明的变量)
    return:成功匹配的参数的个数 如果匹配失败返回i0 如果失败报错或者文件末尾返回EOF
    int fscanf(FILE *__restrict__ __stream, const char *__restrict__ __format, ...)
    */

    char name[50];
    int age;
    char wife[50];
    int scanfR = fscanf(ioFile,"%s %d %s",name,&age,wife);
    while(scanfR != EOF)
    {
        printf("成功匹配到的参数有%d个\n",scanfR);
        printf("%s在%d岁的时候爱上了%s\n",name,age,wife);
        scanfR = fscanf(ioFile,"%s %d %s",name,&age,wife);
    }

    printf("\n");
    
    //关闭文件
    int closeR = fclose(ioFile);
    if (closeR == EOF)
    {
        printf("关闭文件失败");
    }
    

    return 0;
}

MakeFile文件

cpp 复制代码
fscanf_test:fscanf_test.c
	-$(CC) -o $@ $^ 
	-./$@
	-rm ./$@

八、标准输入输出

cpp 复制代码
#include<stdio.h>
#include<stdlib.h>
int main()
{
    char *ch=malloc(100);
    int n;
    //从标准输入中读取数据
    fgets(ch,100,stdin);
    printf("你好:%s\n",ch);

    //标准输出
    fputs(ch,stdout);

    //错误输出
    fputs(ch,stderr);
    return 0;
}

MakeFile文件

cpp 复制代码
stdin_out_err_test:stdin_out_err_test.c
	-$(CC) -o $@ $^ 
	-./$@
	-rm ./$@
相关推荐
AlfredZhao1 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户9718356334661 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪1 天前
linux 拷贝文件或目录到指定的位置
linux
摇滚侠2 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
bush42 天前
嵌入式linux学习记录十四、术语
linux·嵌入式
载数而行5202 天前
Linux 11 动态监控指令top
linux
不会C语言的男孩2 天前
Linux 系统编程 · 第 8 章:进程基础
linux·c语言
古城小栈2 天前
Unix 与 Linux 异同小叙
linux·服务器·unix
凡人叶枫2 天前
Effective C++ 条款42:了解 typename 的双重意义
java·linux·服务器·c++
2601_961875242 天前
决战申论100题2026|最新|范文
linux·容器·centos·debian·ssh·fabric·vagrant