我假设您在Linux环境中,并且您正在命令行终端上使用该程序。
以下是一个简单的解决方案:
#include <fstream>
#include <iostream>
int main (int argc, char **argv) {
int i, line_size;
std::ifstream myfile;
std::string token;
// Check if user put an input file as argument
if(argc < 2){
std::cout << "Usage : ./parser filename" << std::endl;
return 0;
}
// Open file and check for errors
myfile.open(argv[1]);
if(!myfile){
std::cout << "Error opening file" << std::endl;
return -1;
}
// For each line, put the line in variable token
while(getline(myfile, token)){
line_size = token.size();
for(i=2; i < line_size - 4; ++i){
// Go through the line and check for each character if it
// is an uppercase character and if the following one is
// a lowercase character. If so, print it.
if(std::isupper(token[i]) && std::islower(token[i+1])){
std::cout << token.substr(i-2, 6) << std::endl;
}
}
}
myfile.close();
return 0;
}