0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ros c++ python ファイルの読み込み

Last updated at Posted at 2021-12-11

ros c++のプログラム内でファイルを読み込む方法を調べました.
以下サイトのプログラムでできました.

c++

file_read.cpp
# include <fstream>
# include <iostream>
# include <string>

int main()
{
    std::ifstream ifs("./test.txt");

    int buf_size = 81;

    char str[buf_size];
    if (ifs.fail()) {
        std::cerr << "Failed to open file." << std::endl;
        return -1;
    }
    while (ifs.getline(str, buf_size)) {
        std::cout << str << std::endl;
    }
    return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.0.2)
project(linux_mac)

find_package(catkin REQUIRED)

catkin_package(
)

include_directories(
)

add_executable(file_read src/file_read.cpp)
target_link_libraries(
  file_read
)

python2/3

fread.py

import sys

f = open('test.txt', 'r')

data = f.read()

if sys.version_info.major != 2:
  data = data.decode('utf-8')

print(data)

f.close()

実行ディレクトリ内にtest.txtを作成し実行するとファイル内の文字が出力されます.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?