项目 - Qt学生成绩管理系统绘制直方图与折线图(1)

实现1.读取文件内容

#include<QDebug>
#include<QFile>

头文件QFile-用来读写文件,对文件操作

头文件QDebug-用来输出信息,方便调试

读取的文件格式:

数据分别为

代码

打开文件

QFile file("D:/VS刷题/MFCStudentsSystemApp/MFCStudentsSystemApp/StudentInfo.txt");
//if(file.open(QIODevice::ReadWrite)) qDebug()<<"file open"<<endl;
if(!file.open(QIODevice::ReadWrite)) return 0;

 获取行数

char buf[1024];
qint64 lineLength=file.readLine(buf,sizeof(buf));//获取行数

 读取方式1-全部读取:

QByteArray array1=file.readAll();
qDebug()<<array1;

 肉眼找到想要的数据-只要数学,程序成绩即可

 创建变量,可以不用数组

//存成绩的数据
int math;int programming;

 读取方式2-逐行读取,并处理数据

QString data;
QStringList data_s;
bool ok;
if(file.size()!=0)
  {
    QTextStream in(&file);
    while(!in.atEnd())
    {
        data=in.readLine();
        data_s=data.split("\t");
        qDebug()<<data;
        qDebug()<<data_s;
        //math=QString::toInt(data_s[5]); //报错
        math=data_s[5].toInt(&ok);

data的数据 

data_s的数据

ps.两条qDebug同时运行时会出现乱码现象,关掉data会偶尔仍显示乱码,随后恢复

随即判断 

if (math < 60)lower_six_math++;
            else if (math >= 60 && math  < 70)six_seven_math++;
            else if (math  >= 70 && math < 80)seven_eight_math++;
            else if (math  >= 80 && math < 90)eight_night_math++;
            else night_ten_math++;

 程序成绩也一样
 

获取数据结束

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值