ros环境中读取txt文件

ros环境中读取txt文件中的信息,作为输入完成其他任务。上代码:

#include 
#include 
#include 
#include 
#include 

void readMapTxt(const std::string& folderPath)
{
    boost::filesystem::path path(folderPath);
    boost::filesystem::directory_iterator end_itr;

    for (boost::filesystem::directory_iterator itr(path); itr != end_itr; ++itr)
    {
        if (boost::filesystem::is_regular_file(itr->path()) && itr->path().filename() == "map.txt")
        {
            std::string filePath = itr->path().string();
            
            std::ifstream file(filePath);
            if (file.is_open())
            {
                std::string line;
                while (std::getline(file, line))
                {
                    // 在这里可以对每行文本进行处理
                    ROS_INFO_STREAM("Read line: " << line);
                }
                file.close();
            }
            else
            {
                ROS_ERROR_STREAM("Failed to open file: " << filePath);
            }
        }
    }
}

int main(int argc, char** argv)
{
    ros::init(argc, argv, "mapreader");
    ros::NodeHandle nh;

    std::string folderPath = "/home/hjb/test_ros/src/mapreader/map";
    readMapTxt(folderPath);

    ros::spin();
    return 0;
}

你可能感兴趣的:(ros)