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 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

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

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632