IDE为VS2010
1.
编写程序,从键盘输入若干实数(以特殊数值-1结束),分别写到一个文本文件中。要求:数据写入文件时,各数据独自占一行。
输入示例:
输出示例:
#include "stdio.h"
#include "stdlib.h"
int main()
{
FILE *fp;
float num = 0;
fp = fopen("f1.txt", "w+");
while(scanf("%f", &num), num != -1)
{
fprintf(fp,"%f",num);
}
fclose(fp);
return 0;
}
2.
从键盘输入以下5个学生的学号、姓名,以及数学、语文和英语成绩,写到文本文件f2.txt中,再从文件中取出数据,计算每个学生的总成绩和平均分,并将结果显示在屏幕上。
提示:在文件读写的整个过程中,每一次成功的操作都将改变文件指针的位置。当多次打开文件,并需要将读写位置指针定位在文件的首地址时,可使用重定位文件首函数rewind()。调用格式为:
rewind(FIFL *fp);
建议:可将学生信息定义变量如下形式:
输入输出示例:
#include "stdio.h"
#include "stdlib.h"
typedef struct {
int stu_ID;
char stu_Name[10];
int stu_math , stu_chi , stu_eng;
int score_all , score_ave;
}STU;
void Data_In(STU *temp)//结构体变量数据读入
{
scanf("%d", &(temp ->stu_ID));
scanf("%s", temp ->stu_Name);
scanf("%d%d%d", &(temp ->stu_math), &(temp ->stu_chi), &(temp ->stu_eng));
}
int main()
{
FILE *fp;
STU obj;
int index = 0;
fp = fopen("f2.txt", "w+");
for(index = 0; index < 5; index ++)
{
Data_In(&obj);
fprintf(fp, "%d %s %d %d %d\n",
obj.stu_ID, obj.stu_Name, obj.stu_math, obj.stu_chi, obj.stu_eng);
//3050801 陈刚 81 75 82
}
rewind(fp);
printf("\n\n********************\n");
for(index = 0; index < 5; index ++)
{
fscanf(fp, "%d %s %d %d %d",
&obj.stu_ID, obj.stu_Name, &obj.stu_math, &obj.stu_chi, &obj.stu_eng);
obj.score_all = obj.stu_chi + obj.stu_eng + obj.stu_math;
obj.score_ave = obj.score_all / 3;
printf("%d %s %d %d %d %d %d\n",
obj.stu_ID, obj.stu_Name, obj.stu_math, obj.stu_chi, obj.stu_eng,
obj.score_all, obj.score_ave);
}
fclose(fp);
return 0;
}
3.
在第2题的基础上,改进要求:从键盘输入学生信息的个数不做限制,当对应学号信息输入为“-1”时,结束学生信息录入。
#include "stdio.h"
#include "stdlib.h"
typedef struct {
int stu_ID;
char stu_Name[10];
int stu_math , stu_chi , stu_eng;
int score_all , score_ave;
}STU;
void Data_In(STU *temp)//结构体变量数据读入
{
scanf("%d", &(temp ->stu_ID));
if(temp ->stu_ID == -1)
return ;
scanf("%s", temp ->stu_Name);
scanf("%d%d%d", &(temp ->stu_math), &(temp ->stu_chi), &(temp ->stu_eng));
}
int main()
{
FILE *fp;
STU obj;
int index = 0,count = 0;
fp = fopen("f2.txt", "w+");
while(Data_In(&obj),obj.stu_ID != -1)
{
fprintf(fp, "%d %s %d %d %d\n",
obj.stu_ID, obj.stu_Name, obj.stu_math, obj.stu_chi, obj.stu_eng);
count ++;
//3050801 陈刚 81 75 82
}
rewind(fp);
printf("\n\n********************\n");
for(index = 0; index < count; index ++)
{
fscanf(fp, "%d %s %d %d %d",
&obj.stu_ID, obj.stu_Name, &obj.stu_math, &obj.stu_chi, &obj.stu_eng);
obj.score_all = obj.stu_chi + obj.stu_eng + obj.stu_math;
obj.score_ave = obj.score_all / 3;
printf("%d %s %d %d %d %d %d\n",
obj.stu_ID, obj.stu_Name, obj.stu_math, obj.stu_chi, obj.stu_eng,
obj.score_all, obj.score_ave);
}
fclose(fp);
return 0;
}
4、
【拓展题目】假定有个数据文件“cet4.dat”,存储了若干学生的英语四级成绩。每个学生的基本信息结构包括:学号(12位),姓名(不含空格且不超过20位),专业(不超过30位)和英语四级成绩。编写程序,从文件中读取数据,完成下列要求,并将筛选结果输出到屏幕。
(1)按学号序输出:“电科”专业CET-4成绩“优秀”(550分以上)的学生信息,每个学生的信息占一行;
输入输出示例:
由于二进制文件读写对数据格式严苛的要求,此代码仅做示范作用(不一定输出结果如图所示需要看文件的格式是否符合),主要在于掌握二进制读写仅存在于fread和fwrite的函数操作中
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct {
char stu_ID[12];
char stu_Name[3];
char stu_Major[10];
int stu_Score;
}STU;
void sort(STU temp[], int n)
{
int i = 0,j = 0;
STU tem;
for(i = 0; i < n; i ++)
{
for(j = i + 1; j < n; j ++)
{
if(temp[i].stu_ID > temp[j].stu_ID)
{
tem = temp[i];
temp[i] = temp[j];
temp[j] = tem;
}
}
}
}
int main()
{
FILE *fp;
STU obj[101];
int index = 1, n, i = 0;
int temp;
if(fp = fopen("cet-4.dat", "rb+"),fp == NULL)
{
printf("File open error!\n");
}
else
{
while(!feof(fp)){
fread(&obj,sizeof(STU),1,fp);
if(obj[0].stu_Score > 550)
{
obj[index] = obj[0];
index ++;
}
}
n = index - 1;
sort(obj, n);
printf("电科 专业CET-4成绩 优秀:\n");
for(index = 1; index <= n; index ++)
{
printf("%s %s %s %d\n",
obj[index].stu_ID, obj[index].stu_Name, obj[index].stu_Major, obj[index].stu_Score);
}
}
if(fclose(fp) == EOF)
{
printf("File close error!\n");
}
return 0;
}
}
(2)按学号序输出:“软件工程”专业CET-4未通过(425分以下)的学生信息,每个学生的信息占一行。
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct {
char stu_ID[12];
char stu_Name[3];
char stu_Major[10];
int stu_Score;
}STU;
void sort(STU temp[], int n)
{
int i = 0,j = 0;
STU tem;
for(i = 0; i < n; i ++)
{
for(j = i + 1; j < n; j ++)
{
if(temp[i].stu_ID > temp[j].stu_ID)
{
tem = temp[i];
temp[i] = temp[j];
temp[j] = tem;
}
}
}
}
int main()
{
FILE *fp;
STU obj[101];
int index = 1, n, i = 0;
int temp;
if(fp = fopen("cet-4.dat", "rb+"),fp == NULL)
{
printf("File open error!\n");
}
else
{
while(!feof(fp))
{
fread(&obj,sizeof(STU),1,fp);
if(obj[0].stu_Score < 425)
{
obj[index] = obj[0];
index ++;
}
}
n = index - 1;
sort(obj, n);
printf("软件工程 专业CET-4未通过:\n");
for(index = 1; index <= n; index ++)
{
printf("%s %s %s %d\n",
obj[index].stu_ID, obj[index].stu_Name, obj[index].stu_Major, obj[index].stu_Score);
}
}
if(fclose(fp) == EOF)
{
printf("File close error!\n");
}
return 0;
}