Transferred from: http://blog.csdn.net/halibobo520/article/details/3371944
1. Create open File dialog box:
CFileDialog Dlg (True,//true is the Create Open File dialog box, False creates a Save File dialog box
". txt",//the type of the default open file
null,//The file name that is opened by default
Ofn_hidereadonly | ofn_overwriteprompt,//opening a read-only file
"text file (*.txt) |*.txt| All Files (*. *) |*.*| |"); /all file types that can be opened
2. Click OK after opening the file dialog box
if (dlg. DoModal () ==idok)
{
CString M_filepath = dlg. GetPathName ();////////Remove file path
CString M_path;
m_path=m_filepath;//The path of the file into M_path
UpdateData (FALSE);
}
3. Open file:file.open (m_path,cfile::moderead);
4. Progressive reading of files : CStdioFile file;///classes that can read a file line-by-row
CString StrLine;
While (File.readstring (strLine))//////puts each line in the StrLine string
{
Afxmessgebox (strLine);
}
5 Judging the words read out:
strline= "1|2|3|"; /The string to be judged
int strIndex1 = strline.find (' | '); /find "|" in the string
CString a[11];
if ( -1! = strIndex1)//Just find "|" Will not return-1
{
int i=0;
While (-1! = strIndex1)//
{
strIndex1 = strline.find (' | ');
A[i] = Strline.left (STRINDEX1);
strLine = Strline.right (Strline.getlength ()-strindex1-1);
i++;
if (i > 10)//exit cycle
Break ;
}
}
VC Add Open File dialog box and read file