【发布时间】:2017-09-23 21:38:55
【问题描述】:
我最近开始学习 c++,所以我还在学习。基本上我试图在找到字符串“NEW_EMPLOYEE”时读取我的文本文件,然后将每一行存储到它们各自的成员变量中,直到在文本文件中找到一个空行以停止。我遇到的问题是如何使用 getline 将每一行一次导入我的类“Employee”的每个变量中?我应该改用 istringstream 吗?
我的文本文件名为“employee.txt”
NEW_EMPLOYEE
460713
John
Smith
64000
36
END_OF_FILE
我的班级员工:
class Employee {
private: //Member variables
int ID;
string FirstName;
string LastName;
int Salary;
int Hours;
public:
Employee() {} //Default constructor
Employee(int& id, string& firstName, string& lastName, int& salary, int& hours) {
ID = id;
FirstName = firstName;
LastName = lastName;
Salary = salary
Hours = hours;
}
};
我的 main.cpp:
#include <iostream>
#include <fstream>
int main() {
Employee employee;
int id;
string firstName;
string lastName;
int salary;
int hours;
string line;
ifstream employeeFile;
employeeFile.open("employee.txt");
while(getline(employeeFile, line)) {
if(line == "NEW_EMPLOYEE") {
do {
//Don't know what to do here???
} while (!line.empty());
}
}
employeeFile.close();
return 0;
}
【问题讨论】:
标签: c++ class ifstream istringstream