当前位置: 首页 > news >正文

C语言文件读写常用函数

文章目录

    • 1. fopen函数
    • 2. fclose函数
    • 3. fgetc函数
    • 4. fgets函数
    • 5. fputc函数
    • 6. fputs函数
    • 7. fprintf函数
    • 8. fscanf函数
    • 9. fseek函数
    • 10. ftell函数

1. fopen函数

  • 返回值:文件指针(FILE*)
  • 参数:文件名(包括文件路径)、打开模式(例如:"r"表示只读,"w"表示写入,"a"表示追加)
  • 用法:用于打开一个文件,并返回一个文件指针,以便后续的文件操作。
#include <stdio.h>int main() {FILE *file = fopen("example.txt", "r");if (file == NULL) {printf("Failed to open the file.\n");return 1;}// 文件操作...fclose(file);return 0;
}

2. fclose函数

  • 返回值:整数类型(0表示成功,EOF表示失败)
  • 参数:文件指针(FILE*)
  • 用法:用于关闭一个已打开的文件。
#include <stdio.h>int main() {FILE *file = fopen("example.txt", "r");if (file == NULL) {printf("Failed to open the file.\n");return 1;}// 文件操作...fclose(file);return 0;
}

3. fgetc函数

  • 返回值:整数类型(读取的字符的ASCII码值,读取失败返回EOF)
  • 参数:文件指针(FILE*)
  • 用法:用于从文件中读取一个字符。
#include <stdio.h>int main() {FILE *file = fopen("example.txt", "r");if (file == NULL) {printf("Failed to open the file.\n");return 1;}int ch;while ((ch = fgetc(file)) != EOF) {printf("%c", ch);}fclose(file);return 0;
}

4. fgets函数

  • 返回值:字符指针(读取的字符串,读取失败返回NULL)
  • 参数:字符数组、最大读取字符数、文件指针(FILE*)
  • 用法:用于从文件中读取一行字符串。
#include <stdio.h>int main() {FILE *file = fopen("example.txt", "r");if (file == NULL) {printf("Failed to open the file.\n");return 1;}char buffer[100];while (fgets(buffer, 100, file) != NULL) {printf("%s", buffer);}fclose(file);return 0;
}

5. fputc函数

  • 返回值:整数类型(写入的字符的ASCII码值,写入失败返回EOF)
  • 参数:字符、文件指针(FILE*)
  • 用法:用于向文件中写入一个字符。
#include <stdio.h>int main() {FILE *file = fopen("example.txt", "w");if (file == NULL) {printf("Failed to open the file.\n");return 1;}char ch = 'A';fputc(ch, file);fclose(file);return 0;
}

6. fputs函数

  • 返回值:整数类型(成功返回非负值,失败返回EOF)
  • 参数:字符串、文件指针(FILE*)
  • 用法:用于向文件中写入一个字符串。
#include <stdio.h>int main() {FILE *file = fopen("example.txt", "w");if (file == NULL) {printf("Failed to open the file.\n");return 1;}char *str = "Hello, World!";fputs(str, file);fclose(file);return 0;
}

7. fprintf函数

  • 返回值:整数类型(成功返回写入的字符数,失败返回负值)
  • 参数:文件指针(FILE*)、格式化字符串、可变参数
  • 用法:用于按照指定的格式将数据写入文件。
#include <stdio.h>int main() {FILE *file = fopen("example.txt", "w");if (file == NULL) {printf("Failed to open the file.\n");return 1;}int num = 10;fprintf(file, "The number is: %d\n", num);fclose(file);return 0;
}

8. fscanf函数

  • 返回值:整数类型(成功返回读取的项目数,失败返回负值)
  • 参数:文件指针(FILE*)、格式化字符串、可变参数
  • 用法:用于从文件中按照指定的格式读取数据。
#include <stdio.h>int main() {FILE *file = fopen("example.txt", "r");if (file == NULL) {printf("Failed to open the file.\n");return 1;}int num;fscanf(file, "%d", &num);printf("The number is: %d\n", num);fclose(file);return 0;
}

9. fseek函数

  • 返回值:整数类型(0表示成功,非0表示失败)
  • 参数:文件指针(FILE*)、偏移量(long int)、起始位置(int)
  • 用法:用于设置文件指针的位置,实现文件的定位操作。

10. ftell函数

  • 返回值:长整型(当前文件指针的位置)
  • 参数:文件指针(FILE*)
  • 用法:用于获取文件指针的当前位置。

下面是fseek和ftell函数的示例代码:

#include <stdio.h>int main() {FILE *file = fopen("example.txt", "r");if (file == NULL) {printf("Failed to open the file.\n");return 1;}// 使用fseek函数定位文件指针到文件末尾fseek(file, 0, SEEK_END);// 使用ftell函数获取文件指针的当前位置long int size = ftell(file);printf("The size of the file is: %ld bytes\n", size);fclose(file);return 0;
}

在上述示例代码中,首先打开了一个文件,然后使用fseek函数将文件指针定位到文件末尾,接着使用ftell函数获取文件指针的当前位置,即文件的大小。最后关闭文件。

使用fseek函数和ftell函数可以实现文件的定位和随机读写操作。



http://www.lryc.cn/news/162329.html

相关文章:

  • 【C++基础】实现日期类
  • C语言程序设计—通讯录实现
  • 实战:大数据Flink CDC同步Mysql数据到ElasticSearch
  • B-Tree 索引和 Hash 索引的对比
  • 入门Python编程:了解计算机语言、Python介绍和开发环境搭建
  • 深度解析Redisson框架的分布式锁运行原理与高级知识点
  • C#扩展方法
  • uniapp 高度铺满全屏
  • UG\NX二次开发 判断向量在指定的公差内是否为零,判断是否是零向量 UF_VEC3_is_zero
  • 2023年MySQL实战核心技术第一篇
  • hivesql执行过程
  • C语言学习:8、深入数据类型
  • 生成树协议 STP(spanning-tree protocol)
  • 【LeetCode】312.戳气球
  • 商业数据分析概论
  • Golang GUI框架
  • LeetCode刷题笔记【24】:贪心算法专题-2(买卖股票的最佳时机II、跳跃游戏、跳跃游戏II)
  • 游戏出现卡顿有哪些因素
  • 学习Bootstrap 5的第八天
  • vue中自定义指令
  • Python:安装Flask web框架hello world
  • 小程序点击复制功能制作
  • 20230909java面经整理
  • 常用的css命名规则
  • 【Linux编程Shell自动化脚本】03 shell四剑客(find、sed、grep、awk)
  • java的springboot框架中使用logback日志框架使用RabbitHandler注解为什么获取不到消费的traceId信息?
  • 初探Vue.js及Vue-Cli
  • 大数据课程K21——Spark的SparkSQL基础语法
  • 【实践篇】Redis最强Java客户端(三)之Redisson 7种分布式锁使用指南
  • 卫星通话过后,卫星导航产业被彻底激活