通过一次调用 c++ 将整个二进制文件读取到数组中
我正在尝试将二进制文件读入结构数组
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;
}
但是读取导致了段错误,我该如何解决这个问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是错误的:
您要将数据读入向量,您应该调整它的大小,而不仅仅是保留,如下所示:
这也是错误的:
您没有在那里重写向量的数据,而是覆盖了结构本身,还有谁知道还有什么。应该是这样的:
正如尼莫所说,这不太可能提高你的表现。
This is wrong:
You're about to read data into the vector, you should resize it, not just reserve, like this:
This also is wrong:
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:
Like Nemo said though, this is unlikely to improve your performance.
您的
features
的类型是 std::vector 并且将其大小写为 char。类型向量不是数组。Your
features
has type is an std::vector and you casing it to char. Type vector is not an array.我认为您想要
您还需要确保
size
是sizeof(FeaturePoint)
的倍数。否则,你会读得有点太多。I think you want
You also need to make certain that
size
is a multiple ofsizeof(FeaturePoint)
. Otherwise, you will read slightly too much.