VC中文件路径问题
VC中文件路径问题
在VC编程中,文件和路径问题是很重要的。例如,MFC编程中要我们会使用文件类来打开或保存一个文件;编程中要获得某一目录并在该目录下顺序的存储文件;编程中要指定某一目录并顺序读取该目录下的文件等。下面我简要介绍下编程过程中遇到的目录操作。
1.使用文件对话框打开或保存文件
使用文件对话框类CFileDialog。CFileDialog实现了Windows的基本文件对话框。其构造函数原型为: CFileDialog( BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, LPCTSTR lpszFileName = NULL, DWORD dwFlags = OFN_HIDEREADONLY |
OFN_OVERWRITEPROMPT, LPCTSTR lpszFilter = NULL, CWnd* pParentWnd = NULL )。
其中第一个参数为TRUE时,构造一个打开文件对话框对象,为FALSE时构造一个保存文件对话框对象;参数LPCTSTR lpszFilter为打开或保存的类型 常用一个字符串传递。 典型的使用方法如下:
char szPara[] = \"*.bmp|*.bmp|*.*|*.*||\"; UpdateData(TRUE); //保存对话框 打开 CFileDialog
dlg(FALSE/TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szPara);
//注:第一个参数为TRUE为打开,为FALSE为保存 if ( IDOK == dlg.DoModal() ) {
m_BmpPath = dlg.GetPathName(); UpdateData(FALSE); }
注:以上操作仅仅将文件的路径名传递给了参数,具体的打开或保存操作自行完成。
CFileDialog包含的重要成员函数:DoModal
Displays the dialog box and allows the user to make a selection.GetPathName
Returns the full path of the selected file.GetFileName
Returns the filename of the selected file.GetFileExt
Returns the file extension of the selected file.GetFileTitle
Returns the title of the selected file.GetNextPathName
Returns the full path of the next selected file.GetReadOnlyPref
Returns the read-only status of the selected file.GetStartPosition
Returns the position of the first element of the filename list.