【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() 关闭文件。

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

相关推荐
码出钞能力20 分钟前
linux内核模块的查看
linux·运维·服务器
写个博客28 分钟前
暑假算法日记第二天
算法
ChaITSimpleLove43 分钟前
.NET9 实现排序算法(MergeSortTest 和 QuickSortTest)性能测试
算法·排序算法·.net·benchmarkdotnet·datadog.trace
星辰云-1 小时前
# Linux Centos系统硬盘分区扩容
linux·运维·centos·磁盘扩容
CVer儿1 小时前
svd分解求旋转平移矩阵
线性代数·算法·矩阵
Owen_Q1 小时前
Denso Create Programming Contest 2025(AtCoder Beginner Contest 413)
开发语言·算法·职场和发展
聽雨2371 小时前
02每日简报20250704
linux·科技·金融·生活·社交电子·娱乐·媒体
Maki Winster2 小时前
Peek-Ubuntu上Gif录制工具-24.04LTS可装
linux·ubuntu·peek
liulilittle2 小时前
C++ i386/AMD64平台汇编指令对齐长度获取实现
c语言·开发语言·汇编·c++
Wilber的技术分享2 小时前
【机器学习实战笔记 14】集成学习:XGBoost算法(一) 原理简介与快速应用
人工智能·笔记·算法·随机森林·机器学习·集成学习·xgboost