码迷,mamicode.com
首页 > 编程语言 > 详细

C语言文本文件基本操作

时间:2015-11-17 01:38:06      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:

#include<stdio.h>
#include<stdlib.h>
///定义学生结点
typedef struct node
{
    char name[20];
    float CScore;
    float DataBaseScore;
}Student;
//键盘输入学生信息
Student* ScanStudent(int n)
{
    Student *studentArr=(Student*)malloc(sizeof(Student)*n);
    for(int i=0;i<n;i++)
    {
        printf("请输入第%d名学生姓名,C语言成绩,数据库成绩:",(i+1));
        //studentArr[i].name=(char*)malloc(sizeof(char)*20);
        scanf("%s %f %f",&studentArr[i].name,&studentArr[i].CScore,&studentArr[i].DataBaseScore);
    }
    return studentArr;
}
//从文件读入n个学生信息
Student* GetFile(char *filePath,int n)
{
    FILE *fp=fopen(filePath,"r");
    Student *studentArr=new Student[n];
    for(int i=0;i<n;i++)
    {
        fscanf(fp,"姓名:%s  C语言成绩:%f  数据库成绩:%f\n",&studentArr[i].name,&studentArr[i].CScore,&studentArr[i].DataBaseScore);
    }
    fclose(fp);
    return studentArr;
}
//输出学生统计信息到控制台
void PutConsole(float cScore,float dataScore,Student Cmax,Student Cmin,Student dataMax,Student dataMin)
{
    printf("C语言平均分:%f 数据库平均分:%f\n",cScore,dataScore);
    printf("C语言成绩最高:%s %f\nC语言成绩最低:%s %f\n",Cmax.name,Cmax.CScore,Cmin.name,Cmin.CScore);
    printf("数据库成绩最高:%s %f\n数据库成绩最低:%s %f\n",dataMax.name,dataMax.CScore,dataMin.name,dataMin.CScore);
}
//输出学生统计信息到文件
void PutFile(char *filePath,float cScore,float dataScore,Student Cmax,Student Cmin,Student dataMax,Student dataMin)
{
    FILE *fp=fopen(filePath,"a");
    fprintf(fp,"C语言平均分:%f 数据库平均分:%f\n",cScore,dataScore);
    fprintf(fp,"C语言成绩最高:%s %f\nC语言成绩最低:%s %f\n",Cmax.name,Cmax.CScore,Cmin.name,Cmin.CScore);
    fprintf(fp,"数据库成绩最高:%s %f\n数据库成绩最低:%s %f\n",dataMax.name,dataMax.CScore,dataMin.name,dataMin.CScore);
    fclose(fp);
}
//统计成绩信息
void Static(int n,Student *studentArr,float &cScore,float &dataScore,Student &Cmax,Student &Cmin,Student &datamax,Student &datamin)
{
    cScore=0;
    dataScore=0;
    Cmax.CScore=0;
    Cmin.CScore=101;
    datamax.DataBaseScore=0;
    datamin.DataBaseScore=101;
    for(int i=0;i<n;i++)
    {
        cScore+=studentArr[i].CScore;
        dataScore+=studentArr[i].DataBaseScore;
        if(Cmax.CScore<studentArr[i].CScore) Cmax=studentArr[i];
        if(Cmin.CScore>studentArr[i].CScore) Cmin=studentArr[i];
        if(datamax.DataBaseScore<studentArr[i].CScore) datamax=studentArr[i];
        if(datamin.DataBaseScore>studentArr[i].CScore) datamin=studentArr[i];
    }
    cScore=(float)cScore/n;
    dataScore=(float)dataScore/n;
}
//学生信息管理
void InfoManage(char *filePath,Student *studentArr,int n)
{
    FILE *fp=fopen(filePath,"w");
    for(int i=0;i<n;i++)
    {
        char *ar=studentArr[i].name;
        fprintf(fp,"姓名:%s  C语言成绩:%f  数据库成绩:%f\n",studentArr[i].name,studentArr[i].CScore,studentArr[i].DataBaseScore);
    }
    float cScore;
    float dataScore;
    Student Cmax;
    Student Cmin;
    Student dataMax;
    Student dataMin;
    Static(n,studentArr,cScore,dataScore,Cmax,Cmin,dataMax,dataMin);
    PutConsole(cScore,dataScore,Cmax,Cmin,dataMax,dataMin);
    PutFile(filePath,cScore,dataScore,Cmax,Cmin,dataMax,dataMin);
}
//递归函数
void test()
{
    printf("请输入你想统计学生成绩的个数:");
    int n;
    scanf("%d",&n);
    char *putPath="E:\\MyStudent.txt";
    Student *studentArr=GetFile(putPath,n);
    //Student *studentArr=ScanStudent(n);
    InfoManage(putPath,studentArr,n);
    test();
}
void main()
{
    test();

}

 

C语言文本文件基本操作

标签:

原文地址:http://www.cnblogs.com/Huaran1chendu/p/4970506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!