【问题标题】:Read text file into class member variables using getline - c++使用getline将文本文件读入类成员变量 - C++
【发布时间】: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


    【解决方案1】:

    直接的方法是做类似的事情

    while(employeeFile >> line){
        if(line != "NEW_EMPLOYEE") continue;
        int id,salary,hours;
        string firstName, lastName;
        employeeFile >> id >> firstName >> lastName >> salary >> hours;
        Employee employee = Employee(id, firstName, lastName, salary, hours);
        // Do what you want with employee
    }
    

    这假设数据总是以相同的顺序写入文件中。我还假设这些行不包含空格,因为它们不是数字就是名称,所以我使用了&gt;&gt; 运算符。如果不是这种情况,您可以改用getline

    如果您始终确定数据的顺序相同,那么这就足够了。如果不是这样,我建议将对象作为 JSON 写入文件中,并使用 JSON 解析器库将文件直接读取到对象中。

    【讨论】:

    • 每当您执行输入操作时,您必须测试它是否成功 - 您的代码不会这样做。
    • @Neil:我无意为 OP 编写完整的工作代码。我只是在向 OP 展示如何做他们所要求的。我假设输入是有效的,并且做出这个假设是安全的。验证输入是一种很好的编程实践,但超出了问题的范围。
    • 您的代码还有很多其他问题。
    • 没问题。请指出这一点,我很乐意解决它,或提供您自己的答案。
    【解决方案2】:

    是的..直截了当的方法可以帮助您,否则您可以使用简单的方法,例如 ...

    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    #include <sstream>
    using namespace std;
    int main() {
    string x[100];
    int i=0;
    
    //   Employee employee;
    int id;
    string firstName;
    string lastName;
    int salary;
    int hours;
    string line;
    string text;
    
    ifstream employeeFile;
    employeeFile.open("employee.txt");
    while(!employeeFile.eof())
    {
        getline(employeeFile,text);
        x[i++]=text;
    
    
    }
    
    //   employeeFile.close();
    
    stringstream(x[1]) >> id; //string to int 
    firstName =  x[2];
    lastName = x[3];
    
    stringstream(x[4]) >> salary;
    stringstream(x[5]) >> hours;
    
    
    //cout<<id<<" "<<firstName;
    
    
    
    }
    

    然后你可以调用你的方法。但是直截了当的方法比这更完美:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多