功能调用,该函数从文本文件-C 检查输入正确性
Function Call That Checks For Input Correctness From Text File - C++
如果" 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;
}
每当您想根据语法解析/验证输入时,请考虑使用解析器。
写解析器很乏味。因此,请考虑使用解析器。由于您正在编写C ,因此请考虑您即时编译的一种,例如使用Boost Spirit:
活在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 commandn";
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)
相关文章:
- 如何将 UTF-8 文本从文件转换为某个可以迭代的容器,并检查每个符号是否为C++字母数字?
- 如何检查函数是否在LLVM Instrumentation pass的ModulePass的系统头文件中定义?
- 如何检查流中文件的下一个单词是否为 alpha?
- 如何检查文件是否已存在于 fstream C++中
- CocoaPod - 在头文件中使用 C++ 时出现 Podspec 检查错误
- 如何使用 ifstream 检查文件中变量的类型?
- 检查特定txt文件中的项目是否符合"名称编号USACO"中的约束
- 如何检查字符串格式在读取C++文本文件时是否有效?
- 文件存在时的最新检查声明生成输入"is missing"
- 如何检查C++中是否存在文件?
- 检查cpp文件中是否使用了变量
- C++ 如何检查哪些单词不在 2 个类似文件中
- 如何检查文件复制和写入是否成功
- 如何检查二进制文件中是否存在字符串
- 如何在SWIG接口文件中检查Lua版本
- 检查特定目录中是否存在与文件匹配的 abc* 的最佳方法
- 如何检查从键盘输入的特定整数值是否存在于C++文件中的一行或多行中
- 功能调用,该函数从文本文件-C 检查输入正确性
- 如何在C++中进行文件检查
- 使用vim中的语法启用cpp头文件检查