Read the entire binary into an array in a single C ++ call

I am trying to read a binary file into an array of structure

struct FeaturePoint { FeaturePoint (const int & _cluster_id, const float _x, const float _y, const float _a, const float _b ) : cluster_id (_cluster_id), x(_x), y(_y), a(_a), b(_b) {} FeaturePoint (){} int cluster_id; float x; float y; float a; float b; }; 

The code below works, but does this one element at a time by clicking each new element on an array

 void LoadImageFeaturesFromBinaryFile(const char * FileName, std::vector<FeaturePoint>& features ) { char strInputPath[200]; strcpy (strInputPath,"/mnt/imagesearch/tests/"); strcat (strInputPath,FileName); strcat (strInputPath,".bin"); features.clear(); ifstream::pos_type size; ifstream file (strInputPath, ios::in|ios::binary|ios::ate); if (file.is_open()) { size = file.tellg(); cout<< "this file size is : "<<size<<" for "<<strInputPath<<" " <<sizeof( FeaturePoint )<<endl; file.seekg (0, ios::beg); while (!file.eof()) { try { FeaturePoint fp; file.read( reinterpret_cast<char*>(&fp), sizeof( FeaturePoint ) ); features.push_back(fp); } catch (int e) { cout << "An exception occurred. Exception Nr. " << e << endl; } } sort (features.begin(), features.begin()+features.size(),CompareClusterIndexes); file.close(); } } 

I want to speed it up by immediately reading the entire array, which I think should look something like this:

  void LoadImageFeaturesFromBinaryFile(const char * FileName, std::vector<FeaturePoint>& features ) { char strInputPath[200]; strcpy (strInputPath,"/mnt/imagesearch/tests/"); strcat (strInputPath,FileName); strcat (strInputPath,".bin"); features.clear(); ifstream::pos_type size; ifstream file (strInputPath, ios::in|ios::binary|ios::ate); if (file.is_open()) { size = file.tellg(); file.seekg (0, ios::beg); features.reserve( size/sizeof( FeaturePoint )); try { file.read( reinterpret_cast<char*>(&features), size ); } catch (int e) { cout << "An exception occurred. Exception Nr. " << e << endl; } sort (features.begin(), features.begin()+features.size(),CompareClusterIndexes); file.close(); } else cout << strInputPath<< " Unable to open file for Binary read"<<endl; } 

But reading causes a seg error, how can I fix this?

+6
source share
3 answers

It is not right:

 features.reserve( size/sizeof( FeaturePoint )); 

You are going to read data into a vector, you should change its size, and not just reserve, for example:

 features.resize( size/sizeof( FeaturePoint )); 

This is also not true:

 file.read( reinterpret_cast<char*>(&features), size ); 

You do not write over vector data there, you rewrite the structure itself, along with who knows what else. It should be like this:

 file.read( reinterpret_cast<char*>(&features[0]), size ); 

As Nemo said, this is unlikely to improve your performance.

+3
source

The features type is of type std :: vector, and you exchange it for char. The vector type is not an array.

0
source

I think you want

 file.read( reinterpret_cast<char*>(&features[0]), size ); 

You also need to make sure that size multiple of sizeof(FeaturePoint) . Otherwise, you will read too much.

0
source

Source: https://habr.com/ru/post/891419/


All Articles