I've been given a document, from which I have to read certain Information into my C++ program that are preceded by appropriate Keywords looking like this:
...
*useless information*
...
KEYWORD_A : VALUE_A
...
*useless information*
...
KEYWORD_B : VALUE_B
...
What would be the most efficient/correct way for filtering these Keywords, that do not have to appear in a certain order? I currently use following scheme to do so
std::string content_of_line = "";
while(content_of_line.find("KEYWORD_A") == std::string::npos){
std::getline(file,content_of_line);
}
std::stringstream stream(content_of_line);
stream >> variable_a;
std::getline(file,content_of_line);
while(content_of_line.find("KEYWORD_B") == std::string::npos){
std::getline(file,content_of_line);
}
stream.clear();
stream << content_of_line;
stream >> variable_b;
// and so on....
This, however, produces a lot of redundant code and also relies on a specific order of the keywords (which doesnt necessarily have to exist)... I hope you can show me a much nicer way of solving my problem. Thank you very much in advance for trying to help me on my problem!
Ok, I'll take a shot
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_map>
int main()
{
std::ifstream inp("inp.txt");
std::string word;
using valueType = std::string;
//Assuming keywords only appear once in the file
std::unordered_map<std::string, valueType> keywords {{"KEYWORD_A", ""},{"KEYWORD_B", ""},};
while(inp>>std::ws>>word) {
if(keywords.find(word) != keywords.cend()) {
//Assuming schema of lines with keywords is KEY : VALUE
inp.ignore(std::numeric_limits<std::streamsize>::max(), ':');
inp>>std::ws>>keywords[word];
}
}
for(const auto& pair: keywords) {
std::cout<<pair.first<<' '<<pair.second<<'\n';
}
return 0;
}