Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用getline-c++; 我最近开始学习C++,所以我还在学习。基本上,我试图在找到字符串“NEW_EMPLOYEE”时读取文本文件,然后将每一行存储到各自的成员变量中,直到在文本文件中找到一个空行停止。我遇到的问题是如何使用getline一次将每一行导入到类“Employee”的每个变量中?我应该改用istringstream吗_C++_Class_Ifstream_Istringstream - Fatal编程技术网

使用getline-c++; 我最近开始学习C++,所以我还在学习。基本上,我试图在找到字符串“NEW_EMPLOYEE”时读取文本文件,然后将每一行存储到各自的成员变量中,直到在文本文件中找到一个空行停止。我遇到的问题是如何使用getline一次将每一行导入到类“Employee”的每个变量中?我应该改用istringstream吗

使用getline-c++; 我最近开始学习C++,所以我还在学习。基本上,我试图在找到字符串“NEW_EMPLOYEE”时读取文本文件,然后将每一行存储到各自的成员变量中,直到在文本文件中找到一个空行停止。我遇到的问题是如何使用getline一次将每一行导入到类“Employee”的每个变量中?我应该改用istringstream吗,c++,class,ifstream,istringstream,C++,Class,Ifstream,Istringstream,我的文本文件名为“employee.txt” 我的班级员工: 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& lastN

我的文本文件名为“employee.txt”

我的班级员工:

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;
}
#包括
#包括
int main(){
员工;
int-id;
字符串名;
字符串lastName;
国际工资;
整小时;
弦线;
ifstream employeeFile;
employeeFile.open(“employee.txt”);
while(getline(employeeFile,line)){
如果(行==“新员工”){
做{
//不知道在这里做什么???
}而(!line.empty());
}
}
employeeFile.close();
返回0;
}

简单的方法是这样做

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
}
这假设数据总是以相同的顺序写入文件。我还假设这些行不包含空格,因为它们是数字或名称,所以我使用了
>
运算符。如果不是这样,您可以使用
getline


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

是的..直接的方法可以帮助您,否则您可以使用简单的方法,如

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



}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
字符串x[100];
int i=0;
//员工;
int-id;
字符串名;
字符串lastName;
国际工资;
整小时;
弦线;
字符串文本;
ifstream employeeFile;
employeeFile.open(“employee.txt”);
而(!employeeFile.eof())
{
getline(employeeFile,text);
x[i++]=文本;
}
//employeeFile.close();
stringstream(x[1])>>id;//字符串到int
firstName=x[2];
lastName=x[3];
stringstream(x[4])>>工资;
stringstream(x[5])>>小时;

//无论何时执行输入操作,都必须测试输入是否成功-您的代码不会这样做。@Neil:我不打算为OP编写完整的工作代码。我只是向OP演示如何执行他们要求的操作。我假设输入是有效的,并且做出这样的假设是安全的。验证输入是一个很好的程序mming练习,但超出了问题的范围。您的代码有很多其他错误。没问题。请指出,我很乐意修复它,或者提供您自己的答案。
#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;



}