A. 编写程序,从键盘读入10个整数以文本方式写到一个名为f1.dat的新文件中。
#include <stdio.h>
#define N 10
#define error -1
int main()
{
// 定义一个文件指针
FILE *fp1;
// 打开文件,找到文件的地址对其进行写的操作
fp1 = fopen("E://C语言的软件//实验文件//1//f1.dat", "w");
// 检查文件是否打开有误
if (fp1 == NULL)
{
printf("File open error!\n");
return error;
}
else
printf("File open right!\n");
int num[N];
for(int i= 0; i < N ; i++)
{
scanf("%d",&num[i]);// 从键盘输入
/* 将输入为整型的num通过指针fp1写入文件
%d 后面的空格一定加,他的目的是为了
让计算机识别他是10个数据而不是一个 */
fprintf( fp1, "%d ", num[i]);// 往文件fp1写入
}
if( fclose(fp1) )// 判断是否正常关闭
{
printf("Can not close the file!\n");
return error;
}
else
printf("Can close the file!\n");
return 0;
}
B. 编写程序,从键盘读入10个整数以二进制方式写到一个名为f2.dat的新文件中。
#include <stdio.h>
#define N 10
#define error -1
int main()
{
// 定义一个文件指针
FILE *fp;
// 打开文件进行写操作
fp = fopen("E://C语言的软件//实验文件//2//f2.det","wb");
// 判断文件是否能打开
if (fp == NULL)
{
printf("File open error!\n");
return error;
}
else
printf("File open right!\n");
int array[N];
for(int i = 0; i < N ; i++)
{
scanf("%d",&array[i]);// 从键盘输入
/* 找到数组的首地址,判断数据存储类型来判断存储字节的大小,
一次读入(int)个字节,重复1次,将输出的数据存放到文件里面。*/
fwrite(array, sizeof(int), 1, fp);
}
if( fclose(fp) )// 判断是否正常关闭
{
printf("Can not close the file!\n");
return error;
}
else
printf("Can close the file!\n");
return 0;
}
C. 编写程序,从第1题的文件f1.dat中,将数据读出,存入另外一个文件f3.dat中。另外,需要在原本的第5个和第6个整数之间再插入一个整数(这个整数由屏幕输入)一并存入新文件中。
#include <stdio.h>
#define N 10
int main()
{
FILE *fp1, *fp2;
// 打开文件,找到文件的地址对其进行读的操作
fp1 = fopen("E://C语言的软件//实验文件//1//f1.dat", "r");
// 打开文件,找到文件的地址对其进行写的操作
fp2 = fopen("E://C语言的软件//实验文件//3//f3.dat", "w");
int num[N];
for(int i = 0 ; i < N ; i++)
{
fscanf(fp1,"%d",&num[i]);
if( i == N/2 )// 从中间插入数字
{
int insert;
scanf("%d",&insert);
fprintf(fp2,"%d ",insert);
}
fprintf(fp2,"%d ",num[i]);
}
// 关闭文件
fclose(fp1);
fclose(fp2);
return 0;
}
D. 编写程序,在第2题的文件f2.dat中,将数据读出,存入另一个文件f4.dat中。另外,需要在原本的第5个和第6个整数之间再插入一个整数(这个整数由屏幕输入)一并存入新文件中。
#include <stdio.h>
#define N 10
int main()
{
FILE *fp1, *fp2;
// 打开文件,找到文件的地址对其进行读的操作
fp1 = fopen("E://C语言的软件//实验文件//2//f2.dat", "rb");
// 打开文件,找到文件的地址对其进行写的操作
fp2 = fopen("E://C语言的软件//实验文件//4//f4.dat", "wb");
int num[N];
for(int i = 0 ; i < N ; i++)
{
fread(&num[i],sizeof(int),1,fp1);// 以二进制读
}
int copy[N+1];
for(int j = 0 ; j < N+1 ; j++)// 在取出的数组中插入一个数
{
copy[j] = num[j];
if( j == N/2 )
{
int insert;
scanf("%d",&insert);
copy[N/2] = insert;
}
if( j > N/2 )
copy[j] = num[j-1];// 利用copy存储新的数组
}
fwrite(copy,sizeof(int),N+1,fp2);// 以二进制写
return 0;
}
E. 编写程序,建立一个含有N名(N≤5)学生成绩的文件:stu1.dat,每个学生的数据包括:学号、姓名、性别(如下图所示,内容可以简略表示为’M’,或’F’)、语文、数学、体育。
输入数据参考:
解释说明:为了避免测试代码时,反复从屏幕输入数据样例,我这里将数据存在f2.dat文件中,使用freopen()函数访问并读出数据。
输出数据参考:
#include <stdio.h>
#define N 5
typedef struct student
{
int id;
char name[20];
char sex;
int chinese;
int math;
int sport;
}STUDENT;
int main()
{
FILE *fp1, *fp2;
fp1 = freopen("E://C语言的软件//实验文件//5//f2.dat", "r", stdin);
fp2 = fopen("E://C语言的软件//实验文件//5//stu1.det", "w");
STUDENT stu[N];
for(int i=0 ; i < N ; i++)
{
fscanf(fp1,"%d",&stu[i].id);
fgetc(fp1);// 吸收空格
fscanf(fp1,"%s",stu[i].name);
fgetc(fp1);// 吸收空格
fscanf(fp1,"%c",&stu[i].sex);
fscanf(fp1,"%d",&stu[i].chinese);
fscanf(fp1,"%d",&stu[i].math);
fscanf(fp1,"%d",&stu[i].sport);
fprintf(fp2,"%d\t%s\t%c\t%d\t%d\t%d\n",
stu[i].id, stu[i].name, stu[i].sex,
stu[i].chinese, stu[i].math, stu[i].sport);
}
fclose(fp1);
fclose(fp2);
return 0;
}
F. 编写程序,求第5题中每个学生的总分和平均分,并给学生数据增加上总分和平均分,文件名为stu2.dat。
#include <stdio.h>
#define N 5
typedef struct student
{
int id;
char name[20];
char sex;
int chinese;
int math;
int sport;
}STUDENT;
int main()
{
FILE *fp1, *fp2;
fp1 = freopen("E://C语言的软件//实验文件//6//stu1.dat", "r", stdin);
fp2 = fopen("E://C语言的软件//实验文件//6//stu2.det", "w");
STUDENT stu[N];
int sum;
float ave;
for(int i=0 ; i < N ; i++)
{
fscanf(fp1,"%d",&stu[i].id);
fgetc(fp1);// 吸收空格
fscanf(fp1,"%s",stu[i].name);
fgetc(fp1);// 吸收空格
fscanf(fp1,"%c",&stu[i].sex);
fscanf(fp1,"%d",&stu[i].chinese);
fscanf(fp1,"%d",&stu[i].math);
fscanf(fp1,"%d",&stu[i].sport);
sum = ( stu[i].chinese + stu[i].math + stu[i].sport );
ave = sum / 3.0;
fprintf(fp2,"%d\t%s\t%c\t %d\t%d\t%d\t %d\t%.2f\n",
stu[i].id, stu[i].name, stu[i].sex,
stu[i].chinese, stu[i].math, stu[i].sport , sum, ave);
}
fclose(fp1);
fclose(fp2);
return 0;
}
G. 编写程序,对第5题中stu2.dat按总分进行排序,结果存入文件:stu3.dat。
#include <stdio.h>
#define N 5
typedef struct student
{
int id;
char name[20];
char sex;
int chinese;
int math;
int sport;
int sum;
double ave;
}STUDENT;
int main()
{
FILE *fp1, *fp2;
fp1 = freopen("E://C语言的软件//实验文件//7//stu2.dat", "r", stdin);
fp2 = fopen("E://C语言的软件//实验文件//7//stu3.det", "w");
STUDENT stu[N];
for(int i=0 ; i < N ; i++)
{
fscanf(fp1,"%d",&stu[i].id);
fgetc(fp1);// 吸收空格
fscanf(fp1,"%s",stu[i].name);
fgetc(fp1);// 吸收空格
fscanf(fp1,"%c",&stu[i].sex);
fscanf(fp1,"%d",&stu[i].chinese);
fscanf(fp1,"%d",&stu[i].math);
fscanf(fp1,"%d",&stu[i].sport);
fscanf(fp1,"%d",&stu[i].sum);
fscanf(fp1,"%lf",&stu[i].ave);
}
STUDENT temp;
for(int i = 0; i < N-1 ; i++)//冒泡排序
{
for(int j = i+1 ; j < N ; j++)
{
if(stu[j].sum > stu[i].sum)
{
temp = stu[j];
stu[j] = stu[i];
stu[i] = temp;
}
}
}
for(int j = 0 ; j < N ; j++)
{
fprintf(fp1,"%d\t%s\t%c\t%d\t%d\t%d\t%d\t%.2f\n",
stu[j].id, stu[j].name, stu[j].sex,
stu[j].chinese, stu[j].math, stu[j].sport, stu[j].sum, stu[j].ave);
}
fclose(fp1);
fclose(fp2);
return 0;
}