【问题标题】:Unable to find/read config file .conf - FileIOException无法找到/读取配置文件 .conf - FileIOException
【发布时间】:2020-11-02 17:15:17
【问题描述】:

我正在尝试使用 libconfig::readFile() 在我的程序中打开和读取 .conf 文件,但它总是给我一个 FileIOException。我认为程序无法找到该文件,但我不知道问题出在哪里。

这是.conf文件代码:

controlLoopPeriod = 100.0;  # ms

saturationMax = [10.0];
saturationMin = [-10.0];


task = {  Linear_Velocity = {
          type = 0;   # 0 equality, 1 inequality
          gain = 1.0;
          enable = true;
          saturation = 10.0; 
          };
        };


priorityLevels = ( { name = "PL_JPosition";
                     tasks = ["Joint_Position"];
                     lambda = 0.0001;
                     threshold = 0.01; 
                    } 
                  );


actions = ( { name = "jPosition";
              levels = ["PL_JPosition"]; 
            } 
          );


states = {  State_move = {
            minHeadingError = 0.05;
            maxHeadingError = 0.2; 
            };
          };

这是我试图打开文件并阅读它的地方:

bool RobotPositionController::LoadConfiguration() { 
        libconfig::Config confObj;

        // Inizialization
        std::string confPath = "home/nimblbot/tpik_ctr_algo/ctrl_rob_nimblbot";
        confPath.append("/conf/");
        confPath.append(fileName_);

        std::cout << "PATH TO CONF FILE : " << confPath << std::endl;

        // Read the file. If there is an error, report it and exit.
        try {
            confObj.readFile(confPath.c_str());
        } catch (const libconfig::FileIOException& fioex) {
            std::cerr << "I/O error while reading file: " << fioex.what() << std::endl;
            return -1;
        } catch (const libconfig::ParseException& pex) {
            std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine() << " - " << pex.getError() << std::endl;
            return -1;
        }

        conf_->ConfigureFromFile(confObj);

        ConfigureTaskFromFile(tasksMap_, confObj);
        ConfigurePriorityLevelsFromFile(actionManager_, tasksMap_, confObj);

        // Set Saturation values for the iCAT (read from conf file)
        iCat_->SetSaturation(conf_->saturationMin, conf_->saturationMax);

        ConfigureActionsFromFile(actionManager_, confObj);

        //insert states in the map
        statesMap_.insert({ states::ID::jPos, stateJPosition_ });
        
        ConfigureSatesFromFile(statesMap_, confObj);

        //insert command in the map
        commandsMap_.insert({ commands::ID::jPos, commandJPosition_ });
        
        return true;
    }

提前感谢任何类型的帮助。

【问题讨论】:

  • exception.what() 告诉你什么?

标签: c++ readfile libconfig


【解决方案1】:

在我看来,您在主路径的开头缺少/

即改变:

std::string confPath = "home/nimblbot/tpik_ctr_algo/ctrl_rob_nimblbot";

为:

std::string confPath = "/home/nimblbot/tpik_ctr_algo/ctrl_rob_nimblbot";

我认为您已检查 .conf 文件存在于:

/home/nimblbot/tpik_ctr_algo/ctrl_rob_nimblbot/conf

【讨论】:

  • 我已经尝试过使用/home/nimblbot/tpik_ctr_algo/ctrl_rob_nimblbot/conf,但它说我选择的路径中存在语法错误。
  • 它给了我I/O error while reading file: FileIOExceptionAborted (core dumped)。相反,如果我写/home/nimblbot/tpik_ctr_algo/ctrl_rob_nimblbot/conf,则错误是Segmentation fault (core dumped)
  • 好。当然,您需要在路径的开头使用/。如果你阅读你的程序,I/O error while reading file 会发生,因为你的配置文件的路径是错误的。你应该在调用者处处理return -1,所以你避免Aborted;如果一切顺利,您可能正在继续,并且您尝试访问您期望它存在但未创建的对象,并且您的程序最终会出现段错误。无论如何,您现在必须使用/home... 路径并检查为什么您的程序仍然存在段错误。有机会调试吗?
  • 谢谢你是对的,问题出在代码的另一部分。请问您是否可以建议我一个函数来找到正确的路径并避免编写整个路径/home/nimblbot/tpik_ctr_algo/ctrl_rob_nimblbot/conf
  • 您想将配置文件的名称作为参数传递给您的程序并让您的程序在给定文件夹下找到它吗?如果是这样,请查看此帖子:stackoverflow.com/a/612176/260313
猜你喜欢
  • 2017-08-22
  • 1970-01-01
  • 2019-01-08
  • 2013-10-04
  • 1970-01-01
  • 2017-09-16
  • 2019-06-20
  • 2018-03-16
  • 1970-01-01
相关资源
最近更新 更多