【问题标题】:file reading in debian;在 debian 中读取文件;
【发布时间】:2018-03-22 14:44:48
【问题描述】:

我需要读取包含键值对的配置文件。 文件包含几个这样的字符串:

键1=值1
键2=值2
键N=值N

***//终止

class CFG
{
   public:

      static void Init(char* path_);
      static string Param(string name_);
      static string PrintAll();

   private:

      static void GetPathEXE(string path_);
      static void ParseConfigFile();
      static map<string, string> mapData;
};

void CFG::ParseConfigFile()
{
   ifstream file;
   file.open(Param("HomePath") + Param("Slash") + "ConfigMS.cfg");
   if (file.is_open())
   {
      string file_line;
      cmatch results;
      regex reg3("(.*)=(.*)", std::regex_constants::ECMAScript);
      std::cout<<"Parsing cfg file"<<endl;
      while (getline(file, file_line) && (file_line.substr(0, 3) != "***"))
      {
         std::cout<<file_line<<endl;
         std::regex_match(file_line.c_str(), results, reg3);
         mapData.insert(std::pair<string,string>(results.str(1),results.str(2) ));
         mapData[string(results.str(1))] =string( results.str(2));
         std::cout<<"key: "<<results.str(1)<<" val: "<<results.str(2)<<endl;

      }
      //mapData["OuterIP"] = "10.77.1.68";
      std::cout<<"Config loaded\n";
      for (auto it : mapData)
      {
         std::cout<<it.first<<"="<<it.second<<endl;
      }

      if (Param("OuterIP") == "") mapData["Error"] = "IP for receiving messages not set in *.cfg file.\n";
      //else if (data["sipName"] == "") error = "sipName for receiving messages not set in *.cfg file.\n";
   }
   else { mapData["Error"] = "Could not open *.cfg file. Check its existance or name. Name Must \"be ConfigMS.cfg\".\n"; }
   if (Param("RTPPort") == Param("MGCPPort"))
   {
      mapData["Error"] = "RTP port is same as MGCP port. Please change one of them.\n";
   }
   if (Param("Error") != "")
   {
      cout << "\n" + Param("Error");
      //system("pause");
      exit(-1);
   }

}
string CFG::Param(string name_)
{

   return mapData[name_];
}

如果我依赖文件输入,我的地图中有 [OuterIP,10.77.1.68] 键值对,但函数 CFG::Param("OuterIP") 返回空字符串;而且我不知道如何解决这个问题;

【问题讨论】:

  • 如果文件中没有 RTPPortMGCPPort 会发生什么?这行if (Param("RTPPort") == Param("MGCPPort")) 返回true,因为两个空字符串相等。然后下一个if 被执行,你的程序被exit 函数中止。
  • 我有 Init() 函数,它设置了一些默认值,这些行只是检查更改,可能会在配置文件解析后出现。

标签: c++ file debian


【解决方案1】:

我已经编译了您的代码并稍作修改,它可以工作。 rafix07 告诉我们如果没有 MGCPort 和 RTPPort 值,程序将退出,他是对的,但您明确告诉该函数返回一个空字符串。您确定文件中没有另一个空的 OuterIP 参数吗?

顺便说一句,我的修改是删除路径,只读取文件并注释掉 exit(-1) 的东西。 (以及添加一个 main 函数。)

#include <regex>
#include <map>
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

class CFG
{
   public:

      static void Init(char* path_);
      static string Param(string name_);
      static void ParseConfigFile();

   private:

      static void GetPathEXE(string path_);
      static map<string, string> mapData;
};

void CFG::ParseConfigFile()
{
   ifstream file;
   file.open("ConfigMS.cfg");

   if (file.is_open())
   {
      string file_line;
      cmatch results;
      regex reg3("(.*)=(.*)", std::regex_constants::ECMAScript);
      std::cout<<"Parsing cfg file"<<endl;
      while (getline(file, file_line) && (file_line.substr(0, 3) != "***"))
      {
         std::cout<<file_line<<endl;
         std::regex_match(file_line.c_str(), results, reg3);
         mapData.insert(std::pair<string,string>(results.str(1),results.str(2) ));
         mapData[string(results.str(1))] =string( results.str(2));
         std::cout<<"key: "<<results.str(1)<<" val: "<<results.str(2)<<endl;

      }
      //mapData["OuterIP"] = "10.77.1.68";
      std::cout<<"Config loaded\n";
      for (auto it : mapData)
      {
         std::cout<<it.first<<"="<<it.second<<endl;
      }

      if (Param("OuterIP") == "") mapData["Error"] = "IP for receiving messages not set in *.cfg file.\n";
      //else if (data["sipName"] == "") error = "sipName for receiving messages not set in *.cfg file.\n";
   }
   else { mapData["Error"] = "Could not open *.cfg file. Check its existance or name. Name Must \"be ConfigMS.cfg\".\n"; }
   if (Param("RTPPort") == Param("MGCPPort"))
   {
      mapData["Error"] = "RTP port is same as MGCP port. Please change one of them.\n";
   }
   if (Param("Error") != "")
   {
      cout << "\n" + Param("Error");
      //system("pause");
      //exit(-1);
   }

}
string CFG::Param(string name_)
{

   return mapData[name_];
}

map<string, string> CFG::mapData;

int main ()
{
    CFG::ParseConfigFile ();


    cout << "Param: " << CFG::Param("OuterIP") << std::endl;

    return 0;
}

这是我编译的程序:

g++ main.cc -o regex -std=c++11

这是配置文件:

OuterIP=10.0.0.1

结果如下:

Parsing cfg file

key:  val: 

key:  val: 
OuterIP=10.0.0.1
key: OuterIP val: 10.0.0.1
Config loaded
=
OuterIP=10.0.0.1

RTP port is same as MGCP port. Please change one of them.
Param: 10.0.0.1

如你所见,最后一行是 main 中的 cout。

所以您的代码有效。您必须从课程中删除exit(-1)。反正也没啥意义。

【讨论】:

  • 我在测试文件中只有2个字符串,它们的键不相等(OuterIP和MediaPath)
猜你喜欢
  • 1970-01-01
  • 2012-06-08
  • 1970-01-01
  • 2014-09-07
  • 1970-01-01
  • 2013-08-25
  • 2011-07-15
  • 2012-11-16
相关资源
最近更新 更多