прочитать весь двоичный файл в массив в один вызов C ++ - PullRequest
6 голосов
/ 27 июня 2011

Я пытаюсь прочитать двоичный файл в массив структуры

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;
};

Приведенный ниже код работает, но выполняет этот элемент по одному, помещая каждый новый элемент в массив

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();
  }
}

Я хочу ускорить его, читая весь массив за раз, что, я думаю, должно выглядеть примерно так:

    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;
}

Но чтение вызывает ошибку сегмента, как мне это исправить?

Ответы [ 3 ]

3 голосов
/ 27 июня 2011

Это неправильно:

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

Вы собираетесь читать данные в вектор, вы должны изменить их размер, а не просто зарезервировать, как это:

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

Это такженеправильно:

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

Вы не переписываете данные вектора там, вы переписываете саму структуру, а также кто знает, что еще.Это должно быть так:

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

Как сказал Немо, это вряд ли улучшит вашу производительность.

0 голосов
/ 27 июня 2011

Я думаю, что вы хотите

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

Вы также должны убедиться, что size кратно sizeof(FeaturePoint). В противном случае вы будете читать слишком много.

0 голосов
/ 27 июня 2011

Ваш features имеет тип std :: vector, и вы переводите его в char. Тип vector не является массивом.

...