读写bin二进制文件:
读取bin二进制文件:
std::ifstream R_pose_file("../data/pose.bin", std::ios::in | std::ios::binary);
while (!R_pose_file.eof())
{
Pose_R R_temp;
R_pose_file.read(reinterpret_cast<char *>(&R_temp), sizeof(Pose_R));
}
R_pose_file.close();
struct Pose_R
{
Eigen::Matrix4d pose;
double time;
};
写入bin二进制文件:
string save_bin_path = "../pose.bin";
std::ofstream pose_bin(save_bin_path);
pose_bin.write(reinterpret_cast<const char *>(&pose_save), sizeof(Pose_R));
pose_bin.close();
读取txt文件然后储存到bin文件:
void SavePoseTxtToBin(const std::string pose_txt_path, const std::string save_bin_path)
{
std::ifstream pose_txt(pose_txt_path);
std::ofstream pose_bin(save_bin_path);
std::string line, data_in;
int index = 0;
while (getline(pose_txt, line))
{
Pose pose_save;
pose_save.index = index++;
std::istringstream iss(line);
iss >> pose_save.time >> pose_save.pose_x >> pose_save.pose_y >> pose_save.pose_theta;
pose_bin.write(reinterpret_cast<const char *>(&pose_save), sizeof(CartographerPose));
iss.clear();
iss.str("");
}
pose_txt.close();
pose_bin.close();
}
struct Pose {
int index;
double pose_x;
double pose_y;
double pose_theta;
unsigned long long time;
};
读写txt文件:
std::ifstream ground_truth_file("../groundtruth.txt");
std::ofstream pose_voice_file("../pose_voice.txt");
cv::RNG rng;
std::string line, data_in;
while (getline(ground_truth_file, line))
{
// 读取txt文件
std::istringstream input_string_stream(line);
double timestemp, px, py, pz, qx, qy, qz, qw, a_voice, b_voice;
input_string_stream >> timestemp >> px >> py >> pz >> qx >> qy >> qz >> qw;
// 将读取到的数据添加高斯噪声
a_voice = px + rng.gaussian(0.08);
b_voice = py + rng.gaussian(0.08);
// 写入txt文件,并指定精度
pose_voice_file.precision(16);
pose_voice_file << timestemp << " " << a_voice << " " << b_voice << std::endl;
input_string_stream.clear();
input_string_stream.str("");
}
ground_truth_file.close();
pose2d_voice_file.close();
需要添加头文件:
#include <iostream>