c语言学习之路(1)
原文:https://overiq.com/c-programming-101/fread-function-in-c/
---------------------------------------------------------------------------------------------
fread() Function in C
Last updated on July 27, 2020
The fread()
function is the complementary of fwrite()
function. fread()
function is commonly used to read binary data. It accepts the same arguments as fwrite()
function does. The syntax of fread()
function is as follows:
Syntax: size_t fread(void *ptr, size_t size, size_t n, FILE *fp);
The ptr
is the starting address of the memory block where data will be stored after reading from the file. The function reads n
items from the file where each item occupies the number of bytes specified in the second argument. On success, it reads n
items from the file and returns n
. On error or end of the file, it returns a number less than n
.
Let's take some examples:
Example 1: Reading a float value from the file
1 2 3 |
int val;
fread(&val, sizeof(int), 1, fp);
|
This reads a float
value from the file and stores it in the variable val
.
Example 2: Reading an array from the file
1 2 3 |
int arr[10];
fread(arr, sizeof(arr), 1, fp);
|
This reads an array of 10
integers from the file and stores it in the variable arr
.
Example 3: Reading the first 5 elements of an array
1 2 3 |
int arr[10];
fread(arr, sizeof(int), 5, fp);
|
This reads 5
integers from the file and stores it in the variable arr
.
Example 4: Reading the first 5 elements of an array
1 2 3 |
int arr[10];
fread(arr, sizeof(int), 5, fp);
|
This reads 5
integers from the file and stores it in the variable arr
.
Example 5: Reading the structure variable
1 2 3 4 5 6 7 8 9 10 |
struct student
{
char name[10];
int roll;
float marks;
};
struct student student_1;
fread(&student_1, sizeof(student_1), 1, fp);
|
This reads the contents of a structure variable from the file and stores it in the variable student_1
.
Example 6: Reading an array of structure
1 2 3 4 5 6 7 8 9 10 |
struct student
{
char name[10];
int roll;
float marks;
};
struct student arr_student[100];
fread(&arr_student, sizeof(struct student), 10, fp);
|
This reads first 10
elements of type struct student from the file and stores them in the variable arr_student
.
The following program demonstrates how we can use fread()
function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include<stdio.h>
#include<stdlib.h>
struct employee
{
char name[50];
char designation[50];
int age;
float salary
} emp;
int main()
{
FILE *fp;
fp = fopen("employee.txt", "rb");
if(fp == NULL)
{
printf("Error opening file\n");
exit(1);
}
printf("Testing fread() function: \n\n");
while( fread(&emp, sizeof(emp), 1, fp) == 1 )
{
printf("Name: %s \n", emp.name);
printf("Designation: %s \n", emp.designation);
printf("Age: %d \n", emp.age);
printf("Salary: %.2f \n\n", emp.salary);
}
fclose(fp);
return 0;
}
|
Expected Output:
1 2 3 4 5 6 7 8 9 10 11 |
Testing fread() function:
Name: Bob
Designation: Manager
Age: 29
Salary: 34000.00
Name: Jake
Designation: Developer
Age: 34
Salary: 56000.00
|
How it works:
In lines 4-10, a structure employee is declared along with a variable emp
. The structure employee has four members namely: name, designation, age and salary.
In line 14, a structure pointer fp
of type struct FILE
is declared.
In line 15, fopen()
function is called with two arguments namely "employee.txt"
and "rb"
. On success, it returns a pointer to file employee.txt
and opens the file employee.txt
in read-only mode. On failure, it returns NULL
.
In lines 17-21, if statement is used to test the value of fp
. If it is NULL
, printf()
statement prints the error message and program terminates. Otherwise, the program continues with the statement following the if statement.
In lines 25-31, a while loop is used along with fread()
to read the contents of the file. The fread()
function reads the records stored in the file one by one and stores it in the structure variable emp
. The fread()
function will keep returning 1 until there are records in the file. As soon as the end of the file is encountered fread()
will return a value less than 1 and the condition in the while loop become false and the control comes out of the while loop.
In line 33, fclose()
function is used to close the file
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2018-08-16 vim 多窗口,多tab编辑
2018-08-16 【转】php中的会话机制(2)
2017-08-16 【转】三年后再反思我的" Java Web项目管理得失谈"
2017-08-16 java构造函数重载this(true)
2017-08-16 为什么用clojure作为storm 的主要开发语言
2017-08-16 php模拟并发
2017-08-16 【转】storm 开发系列一 第一个程序