按数据块读写文件

函数fread()和fwrite()用于一次读取一组数据,即按数据块读写文件。

fread()函数

函数的原型

1
unsigned int fread(void *buffer, unsigned int size, unsigned int countn, FILE *fp);

功能:fread()函数的功能是从fp所指的文件中读取数据块并存储到buffer指向的内存中。

参数的用途:buffer是待读入数据块的起始地址。size是每个数据块的大小(待读入的每个数据块的字节数)。count是最多允许读取的数据块个数(每个数据块size个字节)。

返回值类型:函数返回的是实际写入的数据块个数

块数据的读写使我们不再局限于一个只读取一个字符、一个单词或一行字符串,它允许用户指定想要读写的内存放大小,最小为1个字节,最大为整个文件。

此实例的程序的前提是在D盘中已经有了一个名为score.txt文件

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include<stdio.h>
#include<stdlib.h>
#define N 30
typedef struct date
{
int year;
int month;
int day;
}DATE;
typedef struct student
{
long studentID;
char studentName[10];
char studentSex;
DATE birthday;
int score[4];
float aver;
}STUDENT;
void InputScore(STUDENT stu[],int n,int m);
void AverScore(STUDENT stu[], int n, int m);
void WritetoFile(STUDENT stu[],int n);
void PrintScore(STUDENT stu[], int n, int m);
int ReadfromFile(STUDENT stu[]);
int main()
{
STUDENT stu[N];
int n, m = 4;
printf("How many student?\n");
scanf("%d",&n);
InputScore(stu,n,m);
AverScore(stu,n,m);
WritetoFile(stu,n);
n = ReadfromFile(stu);
PrintScore(stu, n, m);
return 0;
}
void InputScore(STUDENT stu[],int n,int m)
{
int i,j;
for(i = 0; i < n; i++)
{
printf("Input record %d:\n",i+1);
scanf("%ld",&stu[i].studentID);
scanf("%s",stu[i].studentName);
scanf(" %c",&stu[i].studentSex);
scanf("%d",&stu[i].birthday.year);
scanf("%d",&stu[i].birthday.month);
scanf("%d",&stu[i].birthday.day);
for(j = 0; j < m; j++)
{
scanf("%d", &stu[i].score[j]);
}
}
}
void AverScore(STUDENT stu[], int n, int m)
{
int i, j, sum;
for(i = 0; i < n; i++)
{
sum = 0;
for(j = 0; j < m; j++)
{
sum = sum + stu[i].score[j];
}
stu[i].aver = (float)sum / m;

}
}
void WritetoFile(STUDENT stu[],int n)
{
FILE *fp;
int i,j;
if((fp = fopen("D:\\student.txt","w")) == NULL)
{
printf("Failure to open score.txt!\n");
exit(0);
}
fwrite(stu, sizeof(STUDENT), n, fp);
fclose(fp);
}
int ReadfromFile(STUDENT stu[])
{
FILE *fp;
int i,j;
if((fp = fopen("D:\\student.txt","r")) == NULL)
{
printf("Failure to open score.txt!\n");
exit(0);
}
for(i = 0; !feof(fp); i++)
{
fread(&stu[i],sizeof(STUDENT),1,fp);
}
fclose(fp);
printf("Total student is %d.\n",i-1);

return i - 1;
}
void PrintScore(STUDENT stu[], int n, int m)
{
int i,j;
for( i = 0; i < n; i++)
{
printf("%10ld%8s%3c%6d/%02d/%02d",stu[i].studentID,stu[i].studentName,stu[i].studentSex,
stu[i].birthday.year,stu[i].birthday.month,stu[i].birthday.day);
for(j = 0; j < m; j++)
{
printf("%4d",stu[i].score[j]);
}
printf("%6.1f\n",stu[i].aver);
}

}

/将输入的数据写入名为student.txt的文件中/

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2015-2023 dwx
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信