Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在单次调用c++;_C++_Binaryfiles_Ifstream - Fatal编程技术网

C++ 在单次调用c++;

C++ 在单次调用c++;,c++,binaryfiles,ifstream,C++,Binaryfiles,Ifstream,我试图将一个二进制文件读入一个结构数组 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

我试图将一个二进制文件读入一个结构数组

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&features)
{
字符strInputPath[200];
strcpy(strInputPath,“/mnt/imagesearch/tests/”;
strcat(strInputPath,文件名);
strcat(strInputPath,“.bin”);
特征。清除();
ifstream::pos_类型大小;
ifstream文件(strInputPath,ios::in | ios::binary | ios::ate);
if(file.is_open())
{
size=file.tellg();

cout您的
特性
的类型是一个std::vector,您将其大小写为char。类型vector不是数组。

这是错误的:

features.reserve( size/sizeof( FeaturePoint ));
file.read( reinterpret_cast<char*>(&features),  size );
要将数据读入向量,应调整其大小,而不仅仅是保留,如下所示:

features.resize( size/sizeof( FeaturePoint ));
这也是错误的:

features.reserve( size/sizeof( FeaturePoint ));
file.read( reinterpret_cast<char*>(&features),  size );
file.read(重新解释铸件和特征),尺寸;
你不是在矢量数据上写,你是在覆盖结构本身,还有谁知道还有什么。应该是这样的:

file.read( reinterpret_cast<char*>(&features[0]),  size );
file.read(重新解释强制转换(&features[0]),大小);
正如尼莫所说,这不太可能提高你的表现。

我想你想要的

file.read( reinterpret_cast<char*>(&features[0]),  size );
file.read(重新解释强制转换(&features[0]),大小);

您还需要确保
size
sizeof(FeaturePoint)的倍数
。否则,你会读得有点太多。

我预测,一旦你开始使用它,你会惊讶于它对性能的改善是多么的少。帮你自己一个忙,使用
std::string
而不是
strcat
。对,这会初始化整个(大于缓存)因此,即使磁盘不是瓶颈,这也不会比原始代码快。“保留”加上“推回”通常更快…如果您读取的速度不是内存磁盘的百万倍。