按格式读写文件

C语言允许按指定格式读写文件。函数fscanf()用于按指定格式从文件读数据。

函数原型为:

1
int fscanf(FILE *fp, const char *format,...);

参数作用:第一个参数为文件指针,第二个参数为格式控制参数,第三个参数为地址参数列表,后两个参数和返回值与函数scanf()相同。

fprintf()函数用于指定格式像文件写数据

函数原型为:

1
int fprintf(FILE *fp, const char *format,...);

参数作用:第一个参数为文件指针,第二个参数为格式控制参数,第三个参数为输入参数列表,后两个参数和返回值与函数printf()相同。

**用函数fscanf()和fprintf()进行文件的格式化读写,读写方便,容易理解,但输入时要将ASCLL字符转换成二进制数,输出时,要将二进制数转换为ASCLL字符,比较耗时。

以下实例函数可以实现对学生信息的采集,输入到文件中

需要做的准备为,在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
#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,int m);
int main()
{
STUDENT stu[N];
int n;
printf("How many student?\n");
scanf("%d",&n);
InputScore(stu,n,4);
AverScore(stu,n,4);
WritetoFile(stu,n,4);
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, int m)
{
FILE *fp;
int i,j;
if((fp = fopen("D:\\score.txt","w")) == NULL)
{
printf("Failure to open score.txt!\n");
exit(0);
}
fprintf(fp,"%d\t,%d\n",n,m);
for(i = 0; i < n; i++)
{
fprintf(fp, "%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++)
{

fprintf(fp,"%4d",stu[i].score[j]);
}
fprintf(fp,"%6.1f\n",stu[i].aver);
}
fclose(fp);


}

该程序对文件内容的书写是会改变文件中的内容的

在对程序进行数据输入后,程序就自动退出了,可以通过用户自己到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
#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 ReadfromFile(STUDENT stu[],int *n, int *m);//从文件中读取数据
void PrintScore(STUDENT stu[], int n, int m);//屏幕打印数据
int main()
{
STUDENT stu[N];
int n,m=4;
ReadfromFile(stu, &n, &m);
PrintScore(stu, n, m);
return 0;
}
void ReadfromFile(STUDENT stu[], int *n, int *m)
{
FILE *fp;
int i,j;
if((fp = fopen("D:\\score.txt","r")) == NULL)
{
printf("Failure to open score.txt!\n");
exit(0);
}
fscanf(fp,"%d\t%d",n,m);
for(i = 0; i < *n; i++)
{
fscanf(fp,"%10ld",&stu[i].studentID);
fscanf(fp,"%8s",stu[i].studentName);
fscanf(fp,"% c",&stu[i].studentSex);
fscanf(fp,"%6d/%2d/%2d",&stu[i].birthday.year,
&stu[i].birthday.month,
&stu[i].birthday.day);
for(j = 0; j < 4; j++)
{
fscanf(fp,"%4d", &stu[i].score[j]);
}
fscanf(fp,"%f",&stu[i].aver);
}
fclose(fp);
}
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);
}

}

此程序出了问题,不知问题出在程序还是文件,在深入学习了解后,我会对该程序进行改正

该程序原本的功能是用于对文件中的数据进行提取输出

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:

请我喝杯咖啡吧~

支付宝
微信