c++ how to read a cfg file more complex? -
i changed c# c++. ive been watching tutorials on how read config file. asking wrong, mean is: making program, it's multiple users. every user have prefence on in textfile. example: in textfile ("items.txt"), have default "ints" , configurable "ints".
10=5 90=2 50=9
in c#, if remember correctly, read lines , if line started example "10=", splitted text line configurable int left , can use easly in program, like:
string[] lines = file.readalllines(path); string str; foreach (string line in lines) { if (line.startswith("10")) { str = line.split('=')[1]; //i have need (str); } }
i did needed. so, what's best way in c++?
also: need specific line them, can use them later on in program.
thanks lot in advance!
this code c++ equivalent of c# code posted. code compile, of course have fill in path instead of empty string. if need somewhere in method instead of program on itself, abandon int main() , return 0 of course. reply if unclear. tried set same way c# code structured, understand everything.
#include <fstream> #include <vector> #include <string> int main() { std::string path = ""; std::ifstream file(path); //open file path, might want check file.fail() std::vector<std::string> lines; std::string line; while (file >> line) lines.push_back(line); std::string str; (const std::string& line : lines) { if (line.substr(0, 2).compare("10") == 0) { size_t foundidx = line.find('='); str = line.substr(foundidx + 1); } } file.close(); return 0; }
Comments
Post a Comment