C语言读写文件

C语言读写文件一:打开文件句柄//参数1:文件路径//参数2:文件打开模式函数执行成功返回文件流指针,错误返回NULL。FILE*fopen(constchar*path,constchar*mode);模式操作区别文件要求r读…

大家好,又见面了,我是你们的朋友全栈君。

一:打开文件句柄

//参数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

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/137060.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • sigaction函数解析

    sigaction函数解析sigaction函数的功能是检查或修改与指定信号相关联的处理动作(可同时两种操作)。他是POSIX的信号接口,而signal()是标准C的信号接口(如果程序必须在非POSIX系统上运行,那么就应该使用这个接口)给信号signum设置新的信号处理函数act,同时保留该信号原有的信号处理函数oldactint sigaction(int signo,

    2022年5月25日
  • spel表达式注入[通俗易懂]

    spel表达式注入[通俗易懂]使用parseExpression方法将字符串表达式转换为Expression对象;ParserContext接口用于定义字符串表达式是不是模板,及模板开始与结束字符;

    2022年9月12日
  • 基于视频理解TSM和数据集Kinetics-400的视频行为识别

    基于视频理解TSM和数据集Kinetics-400的视频行为识别这个数据集包括了四百种的人体动作类别,每一种类别都至少有400个视频片段,每个片段都取自不同的Youtube视频,持续大概十秒。数据集的动作类别包括人和物体的交互-比如弹奏乐器;人与人的交互-比如握手。花了好久整理了Kinetics数据集,百度网盘下载地址:…

    2022年6月21日
  • jdbc 事务

    jdbc 事务

    2021年10月3日
  • 集合转成数组的方法_数组转化为集合

    集合转成数组的方法_数组转化为集合1.转换方法1.遍历的方式,依次添加到集合中。2.Arrays.asList()方法3.List.of()方法4.Collections.addAll(集合,数组)方法,将集合存储到数组中1.1遍历的方式这种方式转换成的集合,集合长度可变,可以后续向集合添加数据int[]i={1,5,8,7,11,52};//1.遍历的方式Listlist=newArrayList();for(intx:i){

    2022年9月19日
  • 多线程死锁的产生以及如何避免死锁

    多线程死锁的产生以及如何避免死锁一、死锁的定义多线程以及多进程改善了系统资源的利用率并提高了系统的处理能力。然而,并发执行也带来了新的问题——死锁。所谓死锁是指多个线程因竞争资源而造成的一种僵局(互相等待),若无外力作用,这些进程都将无法向前推进。下面我们通过一些实例来说明死锁现象。先看生活中的一个实例,在一条河上有一座桥,桥面很窄,只能容纳一辆汽车通行。如果有两辆汽车分别从桥的左右两端驶上该桥,则会出现

    2022年7月13日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号