The code below parses the config file. After we successfully load the file, we read it line by line. We remove all the white-spaces from the line and skip the line if it is empty or contains a comment (indicated by "#"). After that, we split the string "name=value" at the delimiter "=" and print the name and the value.
#include <iostream>
#include <fstream>
#include <algorithm>
int main()
{
// std::ifstream is RAII, i.e. no need to call close
std::ifstream cFile ("config2.txt");
if (cFile.is_open())
{
std::string line;
while(getline(cFile, line))
{
line.erase(std::remove_if(line.begin(), line.end(), isspace),
line.end());
if( line.empty() || line[0] == '#' )
{
continue;
}
auto delimiterPos = line.find("=");
auto name = line.substr(0, delimiterPos);
auto value = line.substr(delimiterPos + 1);
std::cout << name << " " << value << '\n';
}
}
else
{
std::cerr << "Couldn't open config file for reading.\n";
}
}