0

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.

Astro - Amit
  • 767
  • 3
  • 15
  • 36
  • possible duplicate of [What is the easiest way to parse an INI File in C++?](http://stackoverflow.com/questions/12633/what-is-the-easiest-way-to-parse-an-ini-file-in-c) – Jonathan Wakely Mar 10 '13 at 17:08

2 Answers2

2

How about using Windows API or Boost Program Options as suggested here What is the easiest way to parse an INI File in C++?

Community
  • 1
  • 1
Rohit Banga
  • 18,458
  • 31
  • 113
  • 191
  • I am not aware of Boost Library and window's API which will b help full on that but i will check the link you have send might be it will be help full. – Astro - Amit Mar 10 '13 at 17:16
2

You're trying to assign a new value to a const string, i.e. change its value. You can't change a const object, it's const.

Make it non-const.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • ahh silly mistake of mine.....thanks for the answer which actually shown my mistake.....And do you have other idea's to parse .ini files? – Astro - Amit Mar 10 '13 at 17:19