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

时间:2017-10-03 01:26:13

标签: c++ ifstream

如果下面的数据是" INSERT_EMPLOYEE"我怎么能检查文本文件?足以输入我的" insertEmployee"功能? (示例:正确的数字,参数类型或检查参数边界)另外,如果输入格式无效,我不想执行操作,而只是跳到下一个操作。

我的文字档案:

INSERT_EMPLOYEE
12345
John
Smith
60000
35

INSERT_EMPLOYEE
Chris
Evans
70000

INSERT_EMPLOYEE
34567
Michael
Carter
50500
25

PRINT_ROSTER

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

1 个答案:

答案 0 :(得分:0)

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

但是,编写解析器很乏味。所以考虑使用解析器生成器。既然你正在编写C ++,那么考虑一下你可以动态编译的那个,比如Boost Spirit:

<强> Live On Coliru

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

对于给定的样本输入,它打印:

Added Employee (12345 John Smith 60000 35)
Ignoring invalid INSERT_EMPLOYEE command
Added Employee (34567 Michael Carter 50500 25)

Roster:
Employee (12345 John Smith 60000 35)
Employee (34567 Michael Carter 50500 25)