It is necessary for the program to compare the version specified in the constant ( const std::string verconst="version=0.2" ) with the version that is stored in the config file. I made reading all the contents from the config, I do not understand how to read a certain part of the text in order to compare it with a constant.

 #include <iostream> #include <unistd.h> #include <stdlib.h> #include <fstream> #define _GNU_SOURCE #include <getopt.h> using namespace std; int main(int argc, char *argv[]) { ifstream confFile; const char path[]="~/.config/lesson3/config.ini"; const std::string verconst="version=0.2"; int opt; std::string config, line,ver; bool isConfig = false; static struct option long_options[] = { {"config", required_argument, 0,'c' }, }; int long_index =0; while ((opt = getopt_long(argc, argv, "c:", long_options, &long_index )) !=-1){ switch(opt){ case 'c': isConfig = true; config = optarg; cout << "Config = " << "[ " << config << " ]" << endl; break; } } if(!isConfig){ config = path; cout << "Config = " << "[ " << config << " ]" << endl; } confFile.open(config); if(confFile.is_open()){ while(getline(confFile, line)){ cout << line << endl; } confFile.close(); } else{ cout << "Can't open file: " << config << endl; exit(1); } return 0; } 

    1 answer 1

    Take any (more precisely, suitable for your format) library for parsing configs and use it. In Windows, EMNIP, standard configs are generally read through WinAPI. And it will turn out something like:

     if( myversion == config.get("version") ) ... 
    • In principle, apparently, the author does not hurt the study of tokenizers / regular expressions / ways to split the string into key / value pairs - strangeqargo
    • one
      So, s/взять готовую/написать свою/ ;) But it will still be useful to look at existing solutions. - PinkTux