如何在CSV文件中读取引号?

时间:2018-03-24 06:23:52

标签: c++ file printing

正如标题所示,我的程序需要读取带引号并用逗号分隔的文件。我是C ++的新手,我不知道该怎么做。它尝试读取的文件是CSV类型的文件。任何帮助都会非常有帮助!文件中的格式为:“Mary”,“000111222”,“Junior”,12,4.0 预期的输出应该是相同的。谢谢!

#include <iostream>
#include <string>
#include <iterator>
#include <iomanip>
#include <fstream>
#include <vector>
#include <sstream>
#include <algorithm>


using namespace std;

class Student {

//declare local variables
protected:
 string name;   
 string ssn;     // Social Secturity Number. 
 string gpa;        //Most up to date gpa for the student
 string credits;    //Number of student's credit hours

                //build public methods
 public:

 // Default Constructor
 Student() {}



Student(const string n, const string s, string sGPA, string sCredits) {

    name = n;
    ssn = s;
    gpa = sGPA;
    credits = sCredits;
}
string getName() {
    return name;
}
string getSSN() {
    return ssn;
}
string getGPA() {
    return gpa;
}
string getCredit() {
    return credits;
}
virtual void print() const {

    cout << '\n' << endl;
    cout << "Student's name: " << name << endl;
    cout << "Student SSN: " << ssn << endl;
    cout << "Student's current GPA: " << gpa << endl;
    cout << "Student's credit hours: " << credits << endl;
}


virtual float tuition() const = 0;
 };

 class Undergrad : public Student {

 //declare local variables

 protected:
 float undergrad_rate = 380.0;
 string year;


//build public methods

 public:


Undergrad() {}


//Undergrad Constructor
Undergrad(const string n, const string s, string uGPA, string uCredits, string y) :
    Student(n, s, uGPA, uCredits), year(y) {}


//Display the contents of undergrad
void print() const {
    Student::print();
    cout << "Undergrad Rate: " << undergrad_rate << endl;
    cout << "Year: " << year << endl;
}


//Display undergrad's current year
string get_year() {
    return year;
}


//Display the undergrad's current rate
float get_rate() {
    return undergrad_rate;
}


//Set a undergrad's current year
void set_year(string y) {
    year = y;
}


//Display the cost for an undergrad to attend university
float tuition() const {
    return 1000000;
}
};

int main() {
ifstream ip("data.txt");

if (!ip.is_open()) std::cout << "ERROR: File not found" << '/n';
string name;
string ssn;
string year;
string credit;
string gpa;

vector<Undergrad> file;
//Undergrad g(name, ssn, year, credit, gpa);
while (ip.good()) {
    getline(ip, name, ',');
    getline(ip, ssn, ',');
    getline(ip, year, ',');
    getline(ip, credit, ',');
    getline(ip, gpa, '\n');

    Undergrad g(name, ssn, year, credit, gpa);
    file.push_back(g);
}

ip.close();
for (int i = 0; i < file.size(); i++) {
    cout << "Name: " << file[i].getName() << endl;
    cout << "SSN: " << file[i].getSSN() << endl;
    cout << "Year: " << file[i].get_year() << endl;
    cout << "Credit:  " << file[i].getCredit() << endl;
    cout << "GPA " << file[i].getGPA() << endl;
    cout << " " << endl;


}

system("pause");
return 0;
 }

0 个答案:

没有答案