我在 1 个函数中打开文件并尝试在其他函数中使用该文件的指针。但我不知道为什么它不工作。下面是代码。
void ReadFile()
{
float data;
int total_rows, pairs;
double longitude, latitude;
{
GsmFingreprintEuc *g;
ll.push_front(new GsmFingreprintEuc);
if(file_ptr.is_open())
cout<<"Yes!!"<<endl;
else
cout<<"NO!!"<<endl;
file_ptr >> data;
total_rows = data;
cout<<"Total Rows:"<<total_rows<<endl;
for (int i = 0; i < total_rows; i++)
{
g = ll.front();
file_ptr >> data;
pairs = data;
for (int j = 0; j < pairs; j++)
{
int id;
double value;
file_ptr >> data;
id = data;
file_ptr >> data;
value = data;
g->add_map(id, value);
}
file_ptr >> data;
latitude = data;
g->set_latitude(latitude);
file_ptr >> data;
longitude = data;
g->set_longitude(longitude);
}
}
cout<<"Size: "<<ll.size()<<endl;
}
DtFileReaderEuc(string file_path)
{
cout << "I am in Constructor" << endl;
cout << file_path << endl;
fstream file_ptr(file_path.c_str(), std::ios_base::in);
if (file_ptr.is_open()) {
cout << "Yahhy!! file Opend successfully" << endl;
float data;
file_ptr >> data;
double total_rows = data;
cout<<"Total Rows:"<<total_rows<<endl;
//file_ptr = myfile;
ReadFile();
//myfile.close();
} else
cout << "Wohoo!! Wrong path" << endl;
cout << "Done!!" << endl;
}
};
当我运行此代码输出时:“我在 Constructor /home/umar/Desktop/DataFile/dha_dataset.gfp Yahhy !! 文件已成功打开 Total Rows:7257 NO!! Total Rows:0 Size: 1 Done!!”
提前致谢