简体   繁体   English

在单个调用c ++中将整个二进制文件读入数组

[英]read entire binary file into an array in single call c++

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 pushing each new element onto 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 reading the entire array in at once, which I think should look something like the following 我想通过立即读取整个数组加快速度,我认为应该看起来像下面这样

    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 the read is causing a seg fault, how do I fix this? 但是读取导致了seg故障,我该如何解决?

This is wrong: 这是错的:

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

You're about to read data into the vector, you should resize it, not just reserve, like this: 您将要将数据读入向量,您应该调整它的大小,而不仅仅是保留,如下所示:

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

This also is wrong: 这也是错的:

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

You're not writing over the vector's data there, you're overwriting the structure itself, along with who knows what else. 你没有在那里写过矢量数据,你要覆盖结构本身,以及谁知道还有什么。 It should be this: 它应该是这样的:

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

Like Nemo said though, this is unlikely to improve your performance. 就像Nemo说的那样,这不太可能改善你的表现。

Your features has type is an std::vector and you casing it to char. 你的features类型是一个std :: vector,你将它包装成char。 Type vector is not an array. 类型矢量不是数组。

I think you want 我想你想要的

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

You also need to make certain that size is a multiple of sizeof(FeaturePoint) . 您还需要确保sizesizeof(FeaturePoint)的倍数。 Otherwise, you will read slightly too much. 否则,你会读得太多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM