c++ - 从文本文件读取到结构 vector ,但文本文件行的长度不同

标签 c++ while-loop ifstream

我正在尝试从学生的文本文件中读取并将每一行分配给一个结构数据成员。

文本文件结构如下:

Name,Student code, Ability,Consistency,Program name:Subject list

文件中的两个学生:

Average Ant,204932,50,5,Short course:1
Brilliant Bison,234543,80,3,Bachelor of Bounciness:2,5,3

除了最后一部分(主题列表),我可以毫无问题地将所有信息读入一个结构,这部分在学生之间的长度各不相同。 我如何编写代码,以便如果一个学生只有 1 个科目,就像普通的 Ant ,我可以将它插入 subjectList vector ,但如果他们有 3 个像聪明的野牛,我仍然可以毫无问题地将它们全部插入?

我的代码:

struct Student
{
    string name;
    int code;
    int ability;
    int consistency;
    string programName;
    vector<int> subjectList;
};

void createStudents(string fileName)
{
    string tempSubjectId;
    int subjectId;

    //temp variable to then use to convert them to int.
    string codeTemp, abilityTemp, consistencyTemp;

    std::ifstream studentFile(fileName);

    //A new student data member is created
    Student newStudent;


    if(studentFile.is_open() && studentFile.good())
    {
        cout << " " << endl;
        cout << "---Reading from Students---" << endl;
        while(getline(studentFile,newStudent.name, ','))
        {

            //we go get each value which is delimited by a comma and one by a colon
            //getline(studentFile, newStudent.name, ',');

            //To convert the strings to an int, the string is given to a temporary variable
            //Then the temporary variable is parsed to an int using stoi and the code datamember from the struct is assign to that new int
            getline(studentFile, codeTemp, ',');
            newStudent.code = stoi(codeTemp);

            getline(studentFile, abilityTemp, ',');
            newStudent.ability = stoi(abilityTemp);

            getline(studentFile, consistencyTemp, ',');
            newStudent.consistency = stoi(consistencyTemp);

            getline(studentFile, newStudent.programName, ':');

//want to push ints into subject list here.


            //The new struct data member is added to the vector and returned for further use.
            studentList.push_back(newStudent);
        }
        //file is then closed
        studentFile.close();```

最佳答案

在主循环中,将整行读入 std::string,然后使用 std::istringstream 解析每一行,使用内部循环来阅读主题整数,例如:

#include <string>
#include <sstream>
#include <fstream>
#include <vector>

struct Student
{
    std::string name;
    int code;
    int ability;
    int consistency;
    std::string programName;
    std::vector<int> subjectList;
}; 

std::vector<Student> studentList;

void createStudents(std::string fileName)
{
    std::string tempLine;

    //temp variable to then use to convert them to int.
    std::string tempStr;

    std::ifstream studentFile(fileName);

    if (studentFile.is_open())
    {
        std::cout << " " << std::endl;
        std::cout << "---Reading from Students---" << std::endl;

        while (std::getline(studentFile, tempLine))
        {
            std::istringstream iss(tempLine);

            //A new student data member is created
            Student newStudent;

            //we go get each value which is delimited by a comma and one by a colon

            std::getline(iss, newStudent.name, ',');

            //To convert the strings to an int, the string is given to a temporary variable
            //Then the temporary variable is parsed to an int using stoi and the code datamember from the struct is assign to that new int
            std::getline(iss, tempStr, ',');
            newStudent.code = std::stoi(tempStr);

            std::getline(iss, tempStr, ',');
            newStudent.ability = std::stoi(tempStr);

            std::getline(iss, tempStr, ',');
            newStudent.consistency = std::stoi(tempStr);

            std::getline(iss, newStudent.programName, ':');

            // push ints into subject list
            while (std::getline(iss, tempStr, ',')) {
                newStudent.subjectList.push_back(std::stoi(tempStr));
            } 

            //The new struct data member is added to the vector and returned for further use.
            studentList.push_back(std::move(newStudent));
        }

        //file is then closed
        studentFile.close();
    }
}

关于c++ - 从文本文件读取到结构 vector ,但文本文件行的长度不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57586519/

相关文章:

php - xmlhttp.responsetext.split while 循环

c++ - ifstream 不占用内存的最佳读取方式

c++ - 如何使用 C 字符串读取 .txt 文件?

c++ - ifstream 无法打开文件

c++ - 在二维数组中添加列

c - fork一个进程,并使用管道将数据从文件逐行传输到子进程

c++ - 如何获取应用程序是否专注于 macOS?

php - 试图在另一个 while 循环中得到一个 while 循环,但它不能一起工作

c++ - 从队列中弹出一个 vector

c++ - 什么 Ruzzle 板包含最独特的单词?