C++多个文本读取问题
生活随笔
收集整理的這篇文章主要介紹了
C++多个文本读取问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
同時使用兩個 ifstream和 freopen?第二個就會失去效用,不知道錯在了哪里!
??????????使用C語言是注意事項,int ret = fscanf(fp,"%lf", &ViewValue);?? lf?和f?有何不同之處?
1. 使用freopen打開:
2. 使用C格式的fopen打開
bool CPicToolsDlg::readTxt2Seq1(std::string TxtName, std::vector<float > &Seq ) {std::string TestTxt(TxtName); // FILE *fp = NULL;if ((fp = fopen(TestTxt.c_str(), "r")) == NULL){printf("Error in open file %s\n", TestTxt.c_str());return false;}else{printf(" file opened%s\n", TestTxt.c_str());}//三元組,使用三元組逐個寫入float ViewValue;Seq.resize(0);//char Temp[100];//if (fin.is_open() )//{// fin.getline(Temp,100);// int i = 0;// while(i< 100 && Temp[i] !="\0" )//尋址級別不同,到底問題出在哪里了!// {// float ViewValue(Temp[i] );// Seq.push_back(ViewValue);// ++ i;// }//}//流出現問題//if (fin.is_open() ) {// while( fin.get(ch) )//尋址級別不同,到底問題出在哪里了!// {// if (ch !=(int)(" ") ){// float ViewValue(ch );// //fin >> ViewValue; //利用 重定向輸入 浮點值 // //std::cin >>ViewValue2; // //std::cin >>ViewValue3; // //P.x = ViewValue;// //P.y = ViewValue2;// //P.z = ViewValue3;// Seq.push_back(ViewValue);// }// }//}while (!feof(fp))//首先讀取點云數據到點表points同時指定點對應的id:{int ret = fscanf(fp,"%f", &ViewValue);Seq.push_back(ViewValue);}//printf("%d\n", n);fclose(fp);//fin.close();//fclose(stdin);return true; }??????????使用C語言是注意事項,int ret = fscanf(fp,"%lf", &ViewValue);?? lf?和f?有何不同之處?
3.修改后的代碼,第一個使用freopen,第二個使用fopen可以正常運行了
void CPicToolsDlg::OnBnClickedButtonKmptest() {//字符串匹配//打開目錄文件CFileDialog FileDlg(TRUE, _T("*.pcd"), NULL,OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,_T("*.txt|*.txt| *.pcd|*.pcd| All Files (*.*) |*.*||"), NULL);//選取約定! FileDlg.m_ofn.lpstrTitle = _T("打開待顯示序列文本!!!"); // 打開文件對話框的標題名if (FileDlg.DoModal() != IDOK) {MessageBox("打開文件失敗!!!請重新檢測", "提示", 0);return ;}CString FilePath = FileDlg.GetPathName();std::string MyFileName = FilePath;std::vector<float > Seq(0);this->readTxt2Seq(MyFileName,Seq);CFileDialog FileDlg2(TRUE, _T("*.pcd"), NULL,OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,_T("*.txt|*.txt| *.pcd|*.pcd| All Files (*.*) |*.*||"), NULL);//選取約定! FileDlg2.m_ofn.lpstrTitle = _T("打開待顯示模式文本!!!"); // 打開文件對話框的標題名if (FileDlg2.DoModal() != IDOK) {MessageBox("打開文件失敗!!!請重新檢測", "提示", 0);return ;}CString FilePath2 = FileDlg2.GetPathName();std::string MyFileName2 = FilePath2;std::vector<float > P(0);this->readTxt2Seq1(MyFileName2,P);const std::vector<float> Pattern(P);int Pos =this->Kmptest(Seq ,Pattern );}
????????
總結
以上是生活随笔為你收集整理的C++多个文本读取问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 时序分析:KMP算法用于序列识别
- 下一篇: s3c2440移植MQTT