希望将文本文件中的逗号分隔值添加到结构的成员

Looking to add comma delimited values in a text file to members of a struct

我这里有一个文本文件,其中包含两个值,一个名称和一个分数。 我有一个学生结构,它有 4 个成员,如下所示。

我希望将文本文件中的值添加到以逗号分隔的结构中的相应成员。

students.txt 文件的前五行;

Nubia,Dufrene,70
Louisa,Trippe,49
Aline,Deniz,34
Shery,Munk,63
Angila,Ping,89

我当前的代码;

struct studentType {
    string studentFName;
    string studentLName;
    int testScore;
    char grade;
};


int main()
{
    vector<studentType> studentVector;
    studentType student;


    ifstream inFile("students.txt");

    while (getline(inFile, student.studentFName, ',' )) {
        cout << student.studentFName << endl;
    }



        printStudents(studentVector);
}


void printStudents(vector<studentType>& passedVect) {

    for (studentType element : passedVect) {
        cout << element.studentFName << " " << element.studentLName << "\tGrade: " << element.testScore << " (" << element.grade << ")" << endl;
    }

}

FIX 我用 for 循环替换了 while 循环。 我还必须将结构从 int 更改为 string 以使其与 getline 一起使用。简单的 convertString 函数使用 std::stoi 将其转换回原计划的 int。


int main()
{
    vector<studentType> studentVector;


    studentType student;

    ifstream inFile("students.txt");

    for (studentType i;
        getline(inFile, i.studentFName, ',')
        && getline(inFile, i.studentLName, ',')
        && getline(inFile, i.testScore)
        ; ) 
    {

        int testScore = convertString(i.testScore);
        i.grade = assignGrade(testScore);

        studentVector.push_back(i);
    }
    printStudents(studentVector);
}


int convertString(string number) {

    return stoi(number);
}

输出

Struct Exercises!
Nubia Dufrene           Grade: 70 (B)
Louisa Trippe           Grade: 49 (D)
Aline Deniz             Grade: 34 (F)
Shery Munk              Grade: 63 (C)
Angila Ping             Grade: 89 (A)
Laila Hollmann          Grade: 10 (F)
Sherrill Piller         Grade: 47 (D)
Minna Dimitri           Grade: 26 (F)
Song Kornreich          Grade: 97 (A)
Frank Dammann           Grade: 36 (F)
Isaac Abee              Grade: 24 (F)
Tiffaney Lukach         Grade: 75 (B)
Carmelina Sink          Grade: 85 (A)
Matthew Benes           Grade: 34 (F)
Fleter Aichele          Grade: 78 (B)
Sergio Ewan             Grade: 56 (C)
Izetta Armes            Grade: 42 (D)
Olen Tee                Grade: 89 (A)
Leona Mozee             Grade: 54 (D)
Britta Pegrast          Grade: 34 (F)

再次感谢!

您可以使用 std::stringstream 先保存每一行,然后使用相同的策略

getline(inFile, student.studentFName, ',' )

除了现在在 stringstream 上填充 3 个变量。现在,Student 已填满,您可以将其 push_back 放入 vector 中,然后调用 printStudents 函数。

我删除了 using namespace std;,因为这是不好的做法(您可以阅读 here 为什么)。你也没有在你的文本文件中提供 grade,所以我在打印学生时删除了那部分。

#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include <sstream> //std::stringstream


struct studentType {
    std::string studentFName;
    std::string studentLName;
    int testScore;
    char grade;
};

void printStudents(const std::vector<studentType>& passedVect) { //also: const&!

    for (studentType element : passedVect) {
        std::cout << element.studentFName << " " << element.studentLName << "\tScore: " << element.testScore << '\n';
    }

}

int main() {
    std::vector<studentType> studentVector;
    studentType student;


    std::ifstream inFile("students.txt");
    if (!inFile.good()) {
        std::cerr << "couldn't find student.txt file\n";
        return -1;
    }

    std::string line;

    while (std::getline(inFile, line)) {
        std::stringstream stream(line);

        std::getline(stream, student.studentFName, ','); //get first name
        std::getline(stream, student.studentLName, ','); //get last name

        std::string score_str; //since testScore is an int, first save it as a string
        std::getline(stream, score_str, ',');
        student.testScore = std::stoi(score_str); //convert string to int

        studentVector.push_back(student); //push it into vector
    }

    printStudents(studentVector);
}

输出:

Nubia Dufrene   Score: 70
Louisa Trippe   Score: 49
Aline Deniz     Score: 34
Shery Munk      Score: 63
Angila Ping     Score: 89
struct studentType {
    string studentFName;
    string studentLName;
    string testScore;
    char grade;
};

void printStudents(vector<studentType>& passedVect );

int main()
{
    vector<studentType> studentVector;
    studentType student;


    ifstream inFile("students.txt");

    while (getline(inFile, student.studentFName, ',' ) 
        && getline(inFile, student.studentLName, ',') 
        && getline(inFile, student.testScore) ) {
        cout << student.studentFName << " " 
             << student.studentLName << " " 
             << student.testScore << endl;
    }
}

我只是将它们连接起来并将 testScore 更改为字符串