【C语言】如何从文件中读取数据?

如何从文件中读取数据?

在C语言中,你可以使用标准库中的函数来从文件中读取数据。下面是一些常用的方法:

使用 fscanf() 读取格式化数据

fscanf() 函数类似于 scanf(),但它从文件中读取数据而不是从标准输入。

c复制代码

|---|--------------------------------------------------------------|
| | #include <stdio.h> |
| | |
| | int main() { |
| | FILE *file; |
| | int number; |
| | float f; |
| | char str[100]; |
| | |
| | file = fopen("data.txt", "r"); |
| | if (file == NULL) { |
| | perror("Error opening file"); |
| | return 1; |
| | } |
| | |
| | // 从文件中读取数据 |
| | while (fscanf(file, "%d %f %s", &number, &f, str) == 3) { |
| | printf("Read: %d, %f, %s\n", number, f, str); |
| | } |
| | |
| | if (ferror(file)) { |
| | perror("Error reading file"); |
| | } else { |
| | printf("End of file reached.\n"); |
| | } |
| | |
| | fclose(file); |
| | return 0; |
| | } |

在上面的代码中,fscanf() 从文件中读取一个整数、一个浮点数和一个字符串,直到文件结束或出现错误。

使用 fgets() 读取一行

fgets() 函数用于从文件中读取一行文本。

c复制代码

|---|----------------------------------------------|
| | #include <stdio.h> |
| | #include <string.h> |
| | |
| | int main() { |
| | FILE *file; |
| | char line[100]; |
| | |
| | file = fopen("text.txt", "r"); |
| | if (file == NULL) { |
| | perror("Error opening file"); |
| | return 1; |
| | } |
| | |
| | // 从文件中读取每一行 |
| | while (fgets(line, sizeof(line), file)) { |
| | printf("%s", line); |
| | } |
| | |
| | if (ferror(file)) { |
| | perror("Error reading file"); |
| | } else { |
| | printf("End of file reached.\n"); |
| | } |
| | |
| | fclose(file); |
| | return 0; |
| | } |

fgets() 将读取一行,包括换行符(如果有的话),并将其存储在提供的缓冲区中。

使用 fread() 读取二进制数据

如果你需要读取二进制数据或大量的数据块,可以使用 fread() 函数。

c复制代码

|---|---------------------------------------------------------------|
| | #include <stdio.h> |
| | |
| | #define BUFFER_SIZE 10 |
| | |
| | int main() { |
| | FILE *file; |
| | int buffer[BUFFER_SIZE]; |
| | size_t itemsRead; |
| | |
| | file = fopen("binary_data.bin", "rb"); |
| | if (file == NULL) { |
| | perror("Error opening file"); |
| | return 1; |
| | } |
| | |
| | // 从文件中读取二进制数据 |
| | itemsRead = fread(buffer, sizeof(int), BUFFER_SIZE, file); |
| | if (itemsRead > 0) { |
| | printf("Read %zu integers: ", itemsRead); |
| | for (size_t i = 0; i < itemsRead; ++i) { |
| | printf("%d ", buffer[i]); |
| | } |
| | printf("\n"); |
| | } |
| | |
| | if (ferror(file)) { |
| | perror("Error reading file"); |
| | } else { |
| | printf("End of file reached.\n"); |
| | } |
| | |
| | fclose(file); |
| | return 0; |
| | } |

在上面的代码中,fread() 尝试从文件中读取 BUFFER_SIZE 个整数。fread() 返回实际读取的项数,这个数可能会少于请求的项数,尤其是当接近文件末尾时。

在使用 fread() 时,你通常需要检查返回值以确保读取了预期数量的项。

注意事项

  • 在读取文件时,总是检查返回值以确保操作成功。
  • 使用 ferror() 函数来检查是否发生了读取错误。
  • 使用 feof() 函数来检查是否已到达文件末尾。
  • 在完成文件操作后,使用 fclose() 关闭文件。

这些是从文件中读取数据的几种基本方法。根据你的具体需求和数据格式,你可能需要选择最适合你的方法。

相关推荐
守望时空332 分钟前
Linux下KDE桌面创建自定义右键菜单
linux
Touper.3 分钟前
JavaSE -- 泛型详细介绍
java·开发语言·算法
sun0077005 分钟前
std::forward作用
开发语言·c++·算法
l0sgAi22 分钟前
vLLM在RTX50系显卡上部署大模型-使用wsl2
linux·人工智能
JoernLee23 分钟前
机器学习算法:支持向量机SVM
人工智能·算法·机器学习
V我五十买鸡腿32 分钟前
顺序栈和链式栈
c语言·数据结构·笔记·算法
我爱一条柴ya1 小时前
【AI大模型】线性回归:经典算法的深度解析与实战指南
人工智能·python·算法·ai·ai编程
麟城Lincoln1 小时前
【RHCSA-Linux考试题目笔记(自用)】servera的题目
linux·笔记·考试·rhcsa
寻月隐君2 小时前
保姆级教程:Zsh + Oh My Zsh 终极配置,让你的 Ubuntu 终端效率倍增
linux·后端·命令行
XM-54582 小时前
2025微信小程序wxapkg解包全攻略
linux·运维·小程序