统计学生的成绩
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//统计学生成绩
typedef struct
{
char no[10];
char name[10];
double foreign;
double spec1;
double spec2;
double total;
} StudentType;
void WriteToFile();
void ReadFromFile();
int main()
{
int select;
do
{
printf("1--录入成绩 2--输出成绩 0--退出\n");
printf("请输入要执行的操作:");
scanf("%d",&select);
switch(select)
{
case 1:WriteToFile();break;
case 2:ReadFromFile();break;
default:printf("退出程序!");break;
}
}while(select==1 || select==2);
return 0;
}
void WriteToFile()
{
FILE *fp=NULL;
StudentType stu;
char flag='y';
fp=fopen("student.txt","a");//"a"是追加的方式
if(fp==NULL)
{
printf("error!");
exit(1);//有错退出 0是无错退出 0以外都是有错退出
}
while((flag=='y')||(flag=='Y'))
{
printf("请输入考生学号:");
scanf("%s",stu.no);
printf("请输入考生姓名:");
scanf("%s",stu.name);
printf("请输入考生的外语成绩:");
scanf("%lf",stu.foreign);
printf("请输入考生专业课1的成绩:");
scanf("%lf",stu.spec1);
printf("请输入考生专业课2的成绩:");
scanf("%lf",stu.spec2);
stu.total=stu.foreign+stu.spec1+stu.spec2;
//写入文件
fprintf(fp,"%10s%10s%8.2f",stu.no,stu.name,stu.foreign);
fprintf(fp,"%8.2f%8.2f%8.2f",stu.spec1,stu.spec2,stu.total);
fputs("\n",fp);
fflush(stdin);//刷新键盘输入的缓冲区
printf("继续输入吗?继续输入yorY:");
scanf("%c",&flag);
}
fclose(fp);
return;
}
/
//统计入学成绩 从文件读出
void ReadFromFile()
{
FILE *fp;
StudentType stu;
char flag='y';
fp=fopen("student.txt","a");//"a"是追加的方式
if(fp==NULL)
{
printf("error!");
exit(1);//有错退出 0是无错退出 0以外都是有错退出
}
printf("考生姓名 总分\n");
while(!feof(fp))//没有遇到文件结束符
{
fscanf(fp,"%s%s",stu.no,stu.name);
fscanf(fp,"%lf%lf%lf%lf",&stu.foreign,&stu.spec1,&stu.spec2,&stu.total);
printf("%10s%8.2f\n",stu.name,stu.total);
}
fclose(fp);
return;
}
文件复制
程序同时打开两个文件,因此,设两个文件指针分别指向两个已打开的文件。
源文件的名字必须要存在,否则打开失败。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void copy(char fileS[],char fileT[]);
int main()
{
char fileS[30],fileT[30];//存放源文件和目标文件名
printf("请输入要复制的源文件:");
scanf("%s",fileS);
printf("请输入要复制的目标文件:");
scanf("%s",fileT);
copy(fileS,fileT);
return 0;
}
/复制函数
void copy(char fileS[],char fileT[])
{
char ch;
FILE *fpSource,*fpTarget;//定义文件指针fpSource和fpTarget
if((fpSource=fopen(fileS,"r")) == NULL)
{
printf("error!\n");
exit(1);
}
if((fpTarget=fopen(fileT,"w")) == NULL)
{
printf("error!\n");
exit(1);
}
while(!feof(fpSource))//当源文件fpSource未结束时执行循环
{
ch=fgetc(fpSource);//ch接收从文件fpSource中读出的一个字符
fputc(ch,fpTarget); //将ch写入目标文件fpTarget
}
fclose(fpSource);
fclose(fpTarget);
printf("文件复制成功!\n");
return;
}
二进制文件的写入/读取
从键盘上输入5个学生的有关结构体数据,然后把他们转存到磁盘文件中。
1.定义一个有5个元素的结构体数组
2.从main中输入学生的数据;调用saveLntoFile函数把学生的数据写入文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 5
/*
A 1 18 P
J 2 20 L
T 3 19 G
M 4 19 O
C 5 34 S*/
struct studentType
{
char name[10];
int num;
int age;
char addr[15];
};
void saveToFile(struct studentType stu[],int num);
//把学生数组中的内容写入文件
void saveToFile(struct studentType stu[],int num)
{
FILE *fp;
int i;
//打开输出文件stu.dat
if((fp=fopen("stu.dat","wb")) == NULL)
{
printf("cannot open file\n");
return;
}
for(i=0;i<num;i++)
{
if(fwrite(&stu[i],sizeof(struct studentType),1,fp)!=1)//向文件写一个数据块
{
printf("file write error!\n");
}
}
fclose(fp);
}
void readFromFile();
//从文件读取所有人学生数据,并显示在屏幕上
void readFromFile()
{
struct studentType student;
int i;
FILE *fp;
//打开stu.dat
if((fp=fopen("stu.dat","rb")) == NULL)//读二进制文件的方式
{
printf("cannot open file\n");
return;
}
//输出问价中所有学生数据
while(!feof(fp))//当文件没有结束的时候
{
if(fread(&student,sizeof(struct studentType),1,fp) == 1)//从文件里读取一个数据块
{
printf("%-10s%4d%4d%-15s\n",student.name,student.num,student.age,student.addr);
}
}
fclose(fp);
}
int main()
{
struct studentType stud[SIZE];
int i;
printf("please enter data of students:\n");
//输入SIZE个学生的数据,存放在数组stud当中
for(i=0;i<SIZE;i++)
{
scanf("%s%d%d%s",stud[i].name,&stud[i].num,&stud[i].age,stud[i].addr);
}
saveToFile(stud,SIZE);
return 0;
}