子函数,删除空格等
std::string& trim(std::string &s)
{
if (s.empty())
{
return s;
}
s.erase(0, s.find_first_not_of(" "));
s.erase(s.find_last_not_of(" ") + 1);
return s;
}
读取主体
std::ifstream configFile("config_file.dat");
if (configFile.is_open()) {
while (configFile.good()) {
std::string str;
std::getline(configFile, str);
str = trim(str);
if (str[0] == '#') {
continue;
}
int mao_idx = str.find_first_of(':');
std::string keyN = str.substr(0, mao_idx);
keyN = trim(keyN);
std::string valN = str.substr(mao_idx + 1, str.size() - 1 - mao_idx);
valN = trim(valN);
std::cout << keyN << "," << valN << "\n";
if (keyN.compare("cam") == 0) capNum = atoi(valN.c_str());
if (keyN.compare("weights") == 0) weiname3 = valN;
if (keyN.compare("cfg") == 0) cfgfile3 = valN;
if (keyN.compare("showVideo") == 0) showVideo = atoi(valN.c_str());
if (keyN.compare("thresh") == 0) thresh = atof(valN.c_str());
if (keyN.compare("camName") == 0) camName = valN;
if (keyN.compare("no_detected_times_thresh") == 0) no_detected_times_thresh = atoi(valN.c_str());
if (keyN.compare("n_m_s") == 0) n_m_s = atof(valN.c_str());
}
std::cout << weiname3 << "," << capNum << "\n";
//std::cout << cfgfile3 << "\n";
}
else {
std::cout << "can't open config_file\n";
}
配置文件如下:
#config file
#注释
weights : ./yolov3-tiny-1-4-2_58000.weights
cfg:./yolov3-tiny-1-4-2.cfg
n_m_s:0.3
no_detected_times_thresh = 9
cam: 0
thresh:0.7
showVideo:1
camName:omc