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

时间:2019-04-02 10:30:32

标签: c++ struct ifstream

我这里有一个文本文件,带有两个值,一个名称和一个分数。 我有一个具有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)

再次感谢!

2 个答案:

答案 0 :(得分:0)

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

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

除了现在在stringstream上填充3个变量。现在,学生已填满,您可以将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

答案 1 :(得分:0)

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更改为字符串