C++ 检查文本文件输入正确性的函数调用-C++;

C++ 检查文本文件输入正确性的函数调用-C++;,c++,ifstream,C++,Ifstream,如果“INSERT_EMPLOYEE”下面的数据足以输入我的“insertEmployee”函数,我如何检查文本文件?(例如:正确的数量、参数类型或检查参数边界)此外,如果输入的格式无效,我不想执行该操作,而只想跳到下一个操作 我的文本文件: INSERT_EMPLOYEE 12345 John Smith 60000 35 INSERT_EMPLOYEE Chris Evans 70000 INSERT_EMPLOYEE 34567 Michael Carter 50500 25 PRI

如果“INSERT_EMPLOYEE”下面的数据足以输入我的“insertEmployee”函数,我如何检查文本文件?(例如:正确的数量、参数类型或检查参数边界)此外,如果输入的格式无效,我不想执行该操作,而只想跳到下一个操作

我的文本文件:

INSERT_EMPLOYEE
12345
John
Smith
60000
35

INSERT_EMPLOYEE
Chris
Evans
70000

INSERT_EMPLOYEE
34567
Michael
Carter
50500
25

PRINT_ROSTER
My main.cpp:

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    int id, salary, hours;
    employeeList list = employeeList();
    string line, firstName, lastName;

    ifstream employeeFile;
    employeeFile.open("employeeFile.txt");
    while(getline(employeeFile, line)) {
        if (line == "INSERT_EMPLOYEE") {
            employeeFile >> id >> firstName >> lastName >> salary >> hours;
            list.insertEmployee(id, firstName, lastName, salary, hours);
        }
        if (line == "PRINT_ROSTER") {
            list.printRoster();
        }
    employeeFile.close();
    return 0;
}
#包括
#包括
使用名称空间std;
int main(){
int id,工资,工时;
employeeList=employeeList();
字符串行,firstName,lastName;
ifstream employeeFile;
打开(“employeeFile.txt”);
while(getline(employeeFile,line)){
如果(行==“插入员工”){
员工档案>>id>>姓氏>>姓氏>>工资>>小时数;
列表。插入员工(id、名字、姓氏、工资、工时);
}
如果(行==“打印花名册”){
list.printloster();
}
employeeFile.close();
返回0;
}

< /代码> 每当您想根据语法解析/验证输入时,请考虑使用解析器。

编写解析器是很乏味的,所以考虑使用分析器生成器。因为你正在编写C++,考虑一下你在编译时编译的一个,就像BoooSoo:

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_match.hpp>
#include <iostream>
#include <fstream>

namespace qi = boost::spirit::qi;

struct Employee {
    unsigned id;
    std::string firstname, surname;
    double salary;
    unsigned hours;
};

static inline std::ostream& operator<<(std::ostream& os, Employee const& emp) {
    return os << "Employee (" 
        << emp.id << " " 
        << emp.firstname << " " 
        << emp.surname << " " 
        << emp.salary << " " 
        << emp.hours << ")";
}

struct employeeList {
    std::vector<Employee> employees;

    void printRoster() const {
        std::cout << "\nRoster:\n";
        for (auto& emp : employees)
            std::cout << emp << "\n";
    }
};

namespace parser {
    using namespace boost::spirit::qi;
    auto const INSERT = copy(uint_ >> eol >> +graph >> eol >> +graph >> eol >> double_ >> eol >> uint_);
}

int main() {
    employeeList list = employeeList();
    std::string line, firstName, lastName;

    std::ifstream input("employeeFile.txt");
    input.unsetf(std::ios::skipws);

    std::string command;
    while (getline(input, command)) {
        if (command == "INSERT_EMPLOYEE") {
            Employee emp;

            if (input >> qi::phrase_match(parser::INSERT, qi::blank, emp.id, emp.firstname, emp.surname, emp.salary, emp.hours)) {
                std::cout << "Added " << emp << "\n";
                list.employees.push_back(emp);
            }
            else {
                std::cout << "Ignoring invalid INSERT_EMPLOYEE command\n";
                input.clear();
                input.ignore(1024, '\n');
            }
        }
        if (command == "PRINT_ROSTER") {
            list.printRoster();
        }

        // skip any non-valid lines until empty line
        while (getline(input, command)) {
            if (command.empty())
                break;
            //else std::cout << "Skipping over input '" << command << "'\n";
        }
    }
}

每当您想根据语法解析/验证输入时,请考虑使用解析器。

编写解析器是很乏味的,所以考虑使用分析器生成器。因为你正在编写C++,考虑一下你在编译时编译的一个,就像BoooSoo:

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_match.hpp>
#include <iostream>
#include <fstream>

namespace qi = boost::spirit::qi;

struct Employee {
    unsigned id;
    std::string firstname, surname;
    double salary;
    unsigned hours;
};

static inline std::ostream& operator<<(std::ostream& os, Employee const& emp) {
    return os << "Employee (" 
        << emp.id << " " 
        << emp.firstname << " " 
        << emp.surname << " " 
        << emp.salary << " " 
        << emp.hours << ")";
}

struct employeeList {
    std::vector<Employee> employees;

    void printRoster() const {
        std::cout << "\nRoster:\n";
        for (auto& emp : employees)
            std::cout << emp << "\n";
    }
};

namespace parser {
    using namespace boost::spirit::qi;
    auto const INSERT = copy(uint_ >> eol >> +graph >> eol >> +graph >> eol >> double_ >> eol >> uint_);
}

int main() {
    employeeList list = employeeList();
    std::string line, firstName, lastName;

    std::ifstream input("employeeFile.txt");
    input.unsetf(std::ios::skipws);

    std::string command;
    while (getline(input, command)) {
        if (command == "INSERT_EMPLOYEE") {
            Employee emp;

            if (input >> qi::phrase_match(parser::INSERT, qi::blank, emp.id, emp.firstname, emp.surname, emp.salary, emp.hours)) {
                std::cout << "Added " << emp << "\n";
                list.employees.push_back(emp);
            }
            else {
                std::cout << "Ignoring invalid INSERT_EMPLOYEE command\n";
                input.clear();
                input.ignore(1024, '\n');
            }
        }
        if (command == "PRINT_ROSTER") {
            list.printRoster();
        }

        // skip any non-valid lines until empty line
        while (getline(input, command)) {
            if (command.empty())
                break;
            //else std::cout << "Skipping over input '" << command << "'\n";
        }
    }
}

您可以使用
std::getline()来实现这一点
,自己验证输入。
格式化的输入提取运算符在输入有效时非常有效。如果无效,您的手上会有一团乱麻,很难解开。因此,如果需要输入验证,请不要使用
>
。故事结束。@SamVarshavchik这很有意义,谢谢!语法提示:“已输入到我的“insertEmployee”函数中?”。请尝试更正式的“按值传递到insertEmployee()函数”,或按引用传递,或者“发送到insertEmployee()函数”或者选择适当时态的同义词:put-in、load、insert;key-in、type-in、enter;code、store。“inputed”是对代码尚未完成的操作的过去时描述。可以使用
,自己验证输入。
格式化的输入提取运算符在输入有效时非常有效。如果无效,您的手上会有一团乱麻,很难解开。因此,如果需要输入验证,请不要使用
>
。故事结束。@SamVarshavchik这很有意义,谢谢!语法提示:“已输入到我的“insertEmployee”函数中?”。请尝试更正式的“按值传递到insertEmployee()函数”,或按引用传递,或者“发送到insertEmployee()函数”或者选择适当时态的同义词:输入、加载、插入;输入、键入、输入;编码、存储。“inputed”是对代码尚未完成的操作的过去时描述。