MFC中讀取檔案方法,遇到空白行解決方案
阿新 • • 發佈:2021-01-12
檔案格式:
程式碼:
void CVoronoiModel::ReadSitesTxtFile()
{
CString filter = "(*.txt)|*txt|所有檔案(*.*)|*.*||";
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, filter, NULL);
if (dlg.DoModal() == IDOK)
{
CString filepath = dlg.GetPathName();
//清理站點
voroSites. clear();
//讀取站點
ifstream in(filepath);
string line;
int index;
float val;
cout << "Reading FEM result from txt... ";
//前兩行表頭資訊,忽略不取
getline(in, line);
getline(in, line);
while (getline(in, line))
{
if (line.empty()) continue;//跳過空白行
vec3 pt;
istringstream record (line);
record >> index;
record >> pt[0];
record >> pt[1];
record >> pt[2];
record >> val;
this->voroSites.push_back(pair<point, float>(pt, val));
}
in.close();
cout << "input... Done. vertices num: " << index << endl;
}
}