m0_74898028 2023-01-06 20:49 采纳率: 80%
浏览 33
已结题

c++读取txt文件

c++读取txt文件读不了

bool Course::readData()
{
    string filename = "CourseScores.txt";
    if (this->_flagReadFile)
    {
        char c;
        cout << "已经读入数据,是否重新读取?(Y/N)" << endl;
        cin >> c;
        if (tolower(c) == 'n')
        {
            return true;  //选择了n,则直接返回,否则往后执行读取数据
        }
    }

    fstream infile;
    infile.open(filename, ios::in);
    if (!infile)
    {
        cout << "工程目录不存在CourseScores.txt文件, 请手动输入文件名. 如: D:/CourseScores.txt" << endl;
        cin >> filename;
        infile.open(filename, ios::in); // 打开新的文件
        if (!infile)
        {
            cout << "输入文件: " << filename << "不存在, 请检查输入." << endl;
            system("pause");
            return 0;
        }
    }

    // 读数据:逐行读取,并调用strmanip.h中的函数进行字符串处理
    string line;
    vector<string> strList;
    string keyword;

    while (!infile.eof())
    {
        getline(infile, line); // 读取一行数据
        strList = strsplit(line, ":,");   // 以:或, 为分割符进行字符串分割

        if (strList.size() == 0) // 空行,则进入下一轮循环读取下一行
            continue;

        keyword = strtrim(strList[0]);  // 提取分割后的第一个字符串

        if ("课程名称" == keyword)
        {
            _courseName = strtrim(strList[1]);  // 提取课程名称
        }
        else if ("课程编号" == keyword)
        {
            _courseNum = strtrim(strList[1]);  // 提取课程编号
        }
        else if ("开课单位" == keyword)
        {
            _courseUnit = strtrim(strList[1]);  // 提取开课单位
        }
        else if ("授课教师" == keyword)
        {
            _courseTeacher = strtrim(strList[1]);  // 提取授课教师
        }
        else if ("选课人数" == keyword)
        {
            _numOfStudents = str2int(strtrim(strList[1]));  // 提取选课人数
        }
        else if ("考勤占比" == keyword)
        {
            pKaoqin = str2double(strtrim(strList[1]));  // 考勤占比
        }
        else if ("作业占比" == keyword)
        {
            pZuoye = str2double(strtrim(strList[1]));  // 作业占比
        }
        else if ("实验占比" == keyword)
        {
            pShiyan = str2double(strtrim(strList[1]));  // 实验占比
        }
        else if ("考试占比" == keyword)
        {
            pKaoshi = str2double(strtrim(strList[1]));  // 考试占比
        }
        else
        {
            if (keyword[0] != '0') // 学号是以0开头的,不以0开头的则该行不是学生信息
                continue;
            Student stu;
            //学号,姓名,性别,班级名称,考勤成绩,作业成绩,实验成绩,考试成绩
            SEX sex = FEMALE;
            if (strtrim(strList[2]) == "男") sex = MALE;
            stu.setData(strtrim(strList[0]), strtrim(strList[1]), sex, strtrim(strList[3]),
                str2double(strtrim(strList[4])), str2double(strtrim(strList[5])),
                str2double(strtrim(strList[6])), str2double(strtrim(strList[7])));
            stu.calculateZhongpinCJ(pKaoqin, pZuoye, pShiyan, pKaoshi);

            _studentInfo.push_back(stu);  // 元素入列
        }

    } // end of while (!infile.eof())

    infile.close(); // 关闭文件

    this->_flagReadFile = true;  // 修改读取文件状态为true

    cout << "数据读取完成!" << endl;
    system("pause");

    return true;
}

img

  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2023-01-06 20:52
    关注

    文件目录不对,用了相对路径读取。txt文件和cpp要在同一个目录下。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月15日
  • 已采纳回答 1月7日
  • 创建了问题 1月6日

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么