1

I want to read a config file using C++

My code is here:

myutils.h

#include <string>
#include <map>
using namespace std;

void read_login_data(char *login_data,map<string,string> &data_map); 

myutils.cpp

#include <fstream>
#include <string>
#include <map>
#include "myutils.h"

using namespace std;

void read_login_data(char *login_data,map<string,string> &data_map)
{
    ifstream infile;
    string config_line;
    infile.open(login_data);
    if (!infile.is_open())
    {
        cout << "can not open login_data";
        return false;

    }
    stringstream sem;
    sem << infile.rdbuf();
    while(true)
    {
        sem >> config_line;
        while(config_line)
        {
            size_t pos = config_line.find('=');
            if(pos == npos) continue;
            string key = config_line.substr(0,pos);
            string value = config_line.substr(pos+1);
            data_map[key]=value;

        }
    }


}

test.cpp:

#include <iostream>
#include <map>
#include "myutils.h"

using namespace std;

int main()
{
    char login[] = "login.ini";
    map <string,string> data_map;

    read_login_data(login,data_map);
    cout<< data_map["BROKER_ID"]<<endl;
    char FRONT_ADDR[20]=data_map["BROKER_ID"].c_str();
    cout << FRONT_ADDR<<endl;
}

The config file is:

BROKER_ID=66666
INVESTOR_ID=00017001033

When I compile it using g++ -o test test.cpp, the output is:

young001@server6:~/ctp/ctp_github/trader/src$ g++ -Wall -o test test.cpp   
test.cpp: In function ‘int main()’:  
test.cpp:23:50: error: array must be initialized with a brace-enclosed initializer

how can I assign the data_map["BROKER_ID"] to FRONT_ADDR?

I have used

strncpy(FRONT_ADDR, data_map["BROKER_ID"].c_str(), sizeof(FRONT_ADDR));

But when I compile it, it says:

young001@server6:~/ctp/ctp_github/trader/src$ g++ -Wall -o test test.cpp   
/tmp/cc5iIQ6k.o: In function `main':  
test.cpp:(.text+0x70): undefined reference to `read_login_data(char*, std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&)'  
collect2: ld returned 1 exit status
1
  • data_map["BROKER_ID"] is a string,so the data_map["BROKER_ID"].c_str() is a c_style string,but it can't direct assign to FRONT_ADDR Commented May 24, 2013 at 2:14

3 Answers 3

3
char FRONT_ADDR[20]=data_map["BROKER_ID"].c_str();

You can't do this. You should use strncpy(or copy_n, but in this case you should check, that length of data_map["BROKER_ID"].c_str() is greater or equal to n) to copy one array of char into another.

std::strncpy(FRONT_ADDR, data_map["BROKER_ID"].c_str(), sizeof(FRONT_ADDR));

And, the best choose, of course, using std::string:

std::string FRONT_ADDR = data_map["BROKER_ID"];
// and, anywhere you need const char*
somefunction(FRONT_ADDR.c_str());
Sign up to request clarification or add additional context in comments.

2 Comments

when I use strncpy,a new problem comes,I have update my question.
You have to compile myutils.cpp too, and then link it. Or just g++ -Wall -o test myutils.cpp test.cpp
1

Try this,

char FRONT_ADDR[20]={0}; //all elements 0
std::strcpy(FRONT_ADDR, data_map["BROKER_ID"].c_str()); // assuming there is enough space in FRONT_ADDR

Comments

0

In C++, there is an inbuilt method(c_str()) to do the conversion. Here's an example.

char* cs[10];
Qstring s("string");
cs = s.c_str();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.