C语言读写文件

一:打开文件句柄

//参数1:文件路径

//参数2:文件打开模式

函数执行成功返回文件流指针,错误返回NULL。

FILE *fopen(const char *path, const char *mode);

 

模式               操作              区别                   文件要求

r                      读            从文件头开始        文件需存在

r+                  读写          从文件头开始        文件需存在

w                    写            从文件头开始        文件不存在则创建,存在则清空

w+                 读写         从文件头开始         文件不存在则创建,存在则清空

a                     写           从文件尾开始         文件不存在进行创建,存在则追加

a+                 读写         从文件头读取,从文件尾写入       文件不存在进行创建,存在则追加

 

代码示例:

#include <stdio.h>

int main()
{
    FILE *p = fopen("/tmp/1.txt", "r");
    if(p == NULL)
    {
        printf("open error!\n");
        return 0;
    }
    fclose(p);
    return 0;
}

 

二:文件关闭

//参数1:文件流

int fclose(FILE *fp);

 

三:文件写入

1、字符写入:fputc();

//参数1:写入的字符

//参数2:文件流

//作用:将单个字符写入到文件中

//返回值:成功时,返回写入字符的ascii码值,错误返回EOF(-1)

int fputc(int c, FILE *stream);

 

示例:

#include <stdio.h>
#include <string.h>

int main()
{
    FILE *file = fopen("/opt/admin/tmp/1.txt", "w");
    if(file == NULL)
    {
        printf("open error!\n");
        return 0;
    }
    char name[] = "hello world!";
    int i;
    for(i = 0; i < strlen(name); i++)
    {
        fputc(name[i], file);
    }
    fputc('\n', file);
    fclose(file);
    return 0;
}

 

2、字符串写入:fputs();

//参数1:写入的字符串

//参数2:文件流

//作用:将字符串写入文件中

//返回值:返回一个非负值,如果发生错误则返回 EOF(-1)。

int fputs(const char *s, FILE *stream);

 

示例:

#include <stdio.h>
#include <string.h>

int main()
{
    FILE *file = fopen("/opt/admin/tmp/1.txt", "w");
    if(file == NULL)
    {
        printf("open error!\n");
        return 0;
    }
    char name[] = "hello world!\n";
    fputs(name, file);
    fclose(file);
    return 0;
}

 

3、数据块写入:fwrite();

//参数1:要获取的数据的地址

//参数2:要写入内容的单字节数

//参数3:要写入size字节的数据项的个数

//参数4:目标文件指针

//返回值:返回实际写入的数据块的数目

//作用:向文件写入数据块,以二进制形式对文件进行操作,不局限于文本文件。

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);

 

示例:

#include <stdio.h>
#include <string.h>

int main()
{
    FILE *file = fopen("/opt/admin/tmp/1.txt", "wb+");
    if(file == NULL)
    {
        printf("open error!\n");
        return 0;
    }
    char name[] = "hello world!\n";
    fwrite(name, strlen(name), 1, file);
    fclose(file);
    return 0;
}

 

4、格式化写入:fprintf();

//参数1:目标文件指针

//参数2:指定的格式控制字符串

//参数3:各种输出项,与格式控制字符串中的字段一起写到文件中

//返回值:执行成功返回实际写入文件的字符个数;执行失败,返回负数

//作用:用来将输出项按指定的格式写入到指定的文本文件中。

int fprintf(FILE *stream, const char *format, ...);

 

示例:

#include <stdio.h>
#include <string.h>

int main()
{
    FILE *file = fopen("/opt/admin/tmp/1.txt", "wb+");
    if(file == NULL)
    {
        printf("open error!\n");
        return 0;
    }
    char name[] = "hello world!\n";
    fprintf(file, "this is %s\n", name);
    fclose(file);
    return 0;
}

四:文件读取

1、字符读取:fgetc()

//参数1:目标文件指针

//返回值:执行成功返回读取的字符,读取错误或者遇到结束标志EOF,返回EOF

//作用:从指定的文件中读取一个字符

int fgetc(FILE *stream);

 

示例:

#include <stdio.h>
#include <string.h>

int main()
{
    FILE *file = fopen("./1.txt", "r");
    if(file == NULL)
    {
        printf("open error!\n");
        return 0;
    }
    char c;
    while((c = fgetc(file)) != EOF)
    {
        printf("%c", c);
    }
    fclose(file);
    return 0;
}

2、字符串读取:fgets()

//参数1:存储读取的数据

//参数2:存储数据的大小

//参数3:要读取的文件流

//返回值:成功则返回读取的buf,失败则返回NULL,这是,buf中的数据不确定

//作用:读取指定场长度的字符串存到字符数组中。每次只读取一行,每次最多读bufsize-1个字符(第bufsize个字符赋'\0')

char *fgets(char *buf, int bufsize, FILE *stream);

 

示例:

#include <stdio.h>
#include <string.h>

int main()
{
    FILE *file = fopen("./1.txt", "r");
    if(file == NULL)
    {
        printf("open error!\n");
        return 0;
    }
    char buf[1024] = {0};
    while(fgets(buf, 1024, file) != NULL)
    {
        printf("%s", buf);
    }
    fclose(file);
    return 0;
}

3、数据块读取:fread()

//参数1:存储读取的数据

//参数2:要读取的每个数据项的字节数

//参数3:要读取的数据项的个数

//参数4:读取的文件流

//返回值:返回真实读取的数据项count数,错误时返回0

//作用:一次读取文件中由若干个数据项组成的数据块,数据块的大小取决于数据项的大小和数据项的个数。

size_t fread ( void *buffer, size_t size, size_t count, FILE *stream) ;

 

示例:

#include <stdio.h>
#include <string.h>

int main()
{
    FILE *file = fopen("./test", "r");
    if(file == NULL)
    {
        printf("open error!\n");
        return 0;
    }
    char buf[102400] = {0};
    fread(buf, 1024, 100, file);
    printf("%s", buf);
    fclose(file);
    return 0;
}

 

4、格式化读取:fscanf()

//参数1:读取的文件流

//参数2:读取的字符格式

//参数3:读取的各个输入项

//返回值:成功则返回读入的参数个数,失败返回EOF

//作用:根据数据格式format从文件流中读取数据,遇到空格或换行结束。

int fscanf(FILE*stream,const char*format,[argument...]);

示例:

#include <stdio.h>
#include <string.h>

int main()
{
    FILE *file = fopen("./1.txt", "r");
    if(file == NULL)
    {
        printf("open error!\n");
        return 0;
    }
    char buf[1024] = {0};
    fscanf(file, "%s", buf);
    printf("%s\n", buf);
    fclose(file);
    return 0;
}

 

五:文件读写结合

1、fgetc()与fputc()结合使用

//以字符格式读取文件,再以字符格式写入文件,适用于文本文件

示例:

#include <stdio.h>
#include <string.h>

int main()
{
    FILE *file = fopen("./1.txt", "r");
    if(file == NULL)
    {
        printf("open error!\n");
        return 0;
    }
    FILE *fp = fopen("./2.txt", "w");
    char c;
    while( (c = fgetc(file)) != EOF )
    {
        fputc(c, fp);
    }
    fclose(file);
    fclose(fp);
    return 0;
}

 

2、fgets()与fputs()结合使用

//从文件中按行读取字符串,再以字符串写入文件,适用于文本文件,优点是按行读取很方便

示例:

#include <stdio.h>
#include <string.h>

int main()
{
    FILE *file = fopen("./1.txt", "r");
    if(file == NULL)
    {
        printf("open error!\n");
        return 0;
    }
    FILE *fp = fopen("./2.txt", "w");
    char buf[1024] = {0};
    while(fgets(buf, 1024, file) != NULL)
    {
        fputs(buf, fp);
    }
    fclose(file);
    fclose(fp);
    return 0;
}

 

3、fread()与fwrite()结合使用

//以数据块格式读取,再以数据块格式写入到文件中,可以读取二进制文件,优点是读取二进制文件使用

示例:

#include <stdio.h>
#include <string.h>

int main()
{
    FILE *file = fopen("./1.txt", "r");
    if(file == NULL)
    {
        printf("open error!\n");
        return 0;
    }
    FILE *fp = fopen("./2.txt", "w");
    char buf[1024] = {0};
    fread(buf, 1024, 1, file);
    fwrite(buf, strlen(buf), 1, fp);
    fclose(file);
    fclose(fp);
    return 0;
}

4、fprintf()与fscanf()结合使用

//以格式化的方式读取,遇到空格或换行就结束,再将读取的文件写入到文件中,优点是可以指定写入的文件格式

示例:

#include <stdio.h>
#include <string.h>

int main()
{
    FILE *file = fopen("./1.txt", "r");
    if(file == NULL)
    {
        printf("open error!\n");
        return 0;
    }
    FILE *fp = fopen("./2.txt", "w");
    char buf[1024] = {0};
    fscanf(file, "%s", buf);
    fprintf(fp, "aaa:%s", buf);
    fclose(file);
    fclose(fp);
    return 0;
}

 

参考:

https://www.cnblogs.com/maluning/p/7955750.html

https://blog.csdn.net/songbook/article/details/79686322

 

 

  • 134
    点赞
  • 738
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值