próbuję odczytać plik binarny do tablicy strukturyodczytać cały plik binarny do tablicy w pojedynczy połączeń 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;
};
poniższy kod prac, ale robi to jeden element na raz, przez naciśnięcie każdego nowym elementem na tablicy
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();
}
}
Chcę ją przyspieszyć czytając całą tablicę na raz, co moim zdaniem powinno wyglądać jak na poniższym
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;
}
Ale odczyt powoduje błąd seg, jak to naprawić?
Przewiduję, że gdy już to zrobisz, będziesz zaskoczony tym, jak niewiele poprawia twoją wydajność. – Nemo
Zrób sobie przysługę i użyj 'std :: string' zamiast' strcat'. –