简体   繁体   中英

Read text file into class member variables using getline - c++

I recently started learning c++ so I'm still learning. Basically I'm trying to read my text file when the string "NEW_EMPLOYEE" is found and then store every line into their respective member variables until an empty line is found in the text file to stop. The problem I'm having is how can I use getline to import each line into each variable of my class "Employee" all at once? Should I use istringstream instead?

My text file called "employee.txt"

NEW_EMPLOYEE
460713
John
Smith
64000
36

END_OF_FILE

My class Employee:

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;
    }
};

My 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;
}

The straightforward approach would be to do something like that

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
}

This assumes that the data are written in the file always in the same order. I also assumed that the lines contain no spaces since they're either numbers or names so I used >> operator. You can use getline instead if that's not the case.

If you always sure the data are in the same order then this should be enough. If that's not true, I'd recommend writing the object as JSON in the file, and use a JSON parser library to read the file directly into an object.

yes..straight forward approach can help you otherwise u can use simple method like ...

#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;



}

Then you can call your method . But straight forward approach is moore perfect than this :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM