I have below code as--->
ConfigFile.h
#ifndef __CONFIG_FILE_H__
#define __CONFIG_FILE_H__
#include <string>
#include <map>
const std::string SECTION1 = "SERVER";
class ConfigFile {
private:
const std::string PortNum;
public:
ConfigFile(std::string const& configFile);
std::string GetPortNO()
{
return PortNum;
}
void Load_Server_Config();
//std::string& operator= (const std::string& str);
};
#endif
ConfigFile.cpp
#include "ConfigFile.h"
#include <fstream>
std::string trim(std::string const& source, char const* delims = " \t\r\n") {
std::string result(source);
std::string::size_type index = result.find_last_not_of(delims);
if(index != std::string::npos)
result.erase(++index);
index = result.find_first_not_of(delims);
if(index != std::string::npos)
result.erase(0, index);
else
result.erase();
return result;
}
ConfigFile::ConfigFile(std::string const& configFile) {
std::ifstream file(configFile.c_str());
std::string temp;
std::string line;
std::string name;
std::string value;
std::string inSection;
int posEqual;
while (std::getline(file,line)) {
if (! line.length()) continue;
if (line[0] == '#') continue;
if (line[0] == ';') continue;
if (line[0] == '[') {
inSection=trim(line.substr(1,line.find(']')-1));
continue;
}
posEqual=line.find('=');
name = trim(line.substr(0,posEqual));
value = trim(line.substr(posEqual+1));
if (name.compare("Port") == 0)
{
PortNum = value;
}
}
}
int main()
{
ConfigFile cf("test.ini");
return 0;
}
.ini file..
[SERVER]
Port = 1234
where PortNum is the member of class ConfigFile above code gives me a compilation error as error C2678: binary '=' : no operator its due to there is no "=" overload operator not present in my class...so how can i overload "=" operator for My class.....or is there any way to copy/assign string value in another...
The above code is written to read a .ini file in which if config Port is present then i will be coping the value in PortNum..
Adding to my question what other way i can prefer to load .ini file.