Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

fstream - How to set the precision while writing binary file in c++?

I need to get the data in .pcd format into .bin format(data is lidar point clouds). So I am trying to write the data in a row aligned order with precision. But I could able to set the precision at all. After doing the conversion, I read the .bin file. data is still not in the pre defined precision format. Here is the code:

#include <iostream>           
#include <pcl/io/pcd_io.h>      
#include <pcl/point_types.h>     
using namespace std;


int main(){
    std::string bin_path = "/home/dummy/data1/pcd_bin/bin/";
    std::string out_file = bin_path+"100.bin";
    pcl::PointCloud<pcl::PointXYZ> cloud;
    for (int v=0; v<1000; ++v)
    {
        pcl::PointXYZ newPoint;
        newPoint.x = (rand() * 100.0) / RAND_MAX;
        newPoint.y = (rand() * 100.0) / RAND_MAX;
        newPoint.z = (rand() * 100.0) / RAND_MAX;
        //newPoint.intensity = (rand() * 100.0) / RAND_MAX;
        cloud.points.push_back(newPoint);
    }
       //Create & write .bin file
      ofstream  bin_file(out_file.c_str(),ios::out|ios::binary|ios::app);
      if(!bin_file.good()) cout<<"Couldn't open "<<out_file<<endl;  


      for (size_t i = 0; i < cloud.points.size (); ++i)
      {
        bin_file<<std::setprecision(5);
        bin_file.write((char*)&cloud.points[i].x,3*sizeof(float)); 
       // bin_file.write((char*)&cloud.points[i].intensity,sizeof(float));
        //cout<<    cloud->points[i]<<endl;
      }
}

As you can see writing the values to binary files in float32(assumption, after reading some stackoverflow questions) and reading them in python code as float32 only.

Expected saved format in binary file:

[84.0187 39.438 78.309 ]

[79.844 91.164 19.755]

[33.522 76.822 27.777]

[55.396 47.739 62.887]

[36.478 51.340 95.222 ]

But actual saved format in binary file same as original format:

[84.01877 39.438293 78.30992 ]

[79.844 91.164734 19.755136]

[33.522274 76.82296 27.777472]

[55.396996 47.739704 62.887093]

[36.478447 51.34009 95.22297 ]

Python code for reading the binary file:

 raw_lidar_o_crop = np.fromfile(100.bin, dtype=np.float32).reshape((-1, 4))
    print("ouster new ")
    with open("ouster_new_intensity2.txt", "w") as text_file:
        for i in range(len(raw_lidar_o_crop)):
           print(f"ouster points cropped : {raw_lidar_o_crop[i]}", file=text_file)

PS: expected format values are dummy. I read the binary file in python and saved the values in .txt file for visualization.

PS1: One need pcl library to reproduce the result.

PS2: I have other binary files written in C++ with precision, so I am reading those files in python with type float32.

Thanks in advance

question from:https://stackoverflow.com/questions/65872721/how-to-set-the-precision-while-writing-binary-file-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As @Some programmer dude suggested in the above comments, we can use std::setprecision only when we need to present the values from binary files with precision. In order to set the precision first we need to do the store the values to .txt file with precision and store the values from .txt to .bin file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.7k users

...