For example:ide
UpdateData(TRUE); CFileDialog dlg(1,NULL,NULL,OFN_HIDEREADONLY ,"All Files(*.*)|*.*||"); if(IDOK!=dlg.DoModal())return; m_filename=dlg.GetPathName(); UpdateData(FALSE);
When user click browse button, the function UpdateData(TRUE)
will refresh the value from controls to variables. As the same reason, the function UpdateData(FALSE)
will refresh the value from variables to controls.rest
#How to open a file with a dialog? The code CFileDialog dlg(1,NULL,NULL,OFN_HIDEREADONLY ,"All Files(*.*)|*.*||");
is supposed to open any files. The following hyperlink contains how to use the CFileDialog member function.code
MSDN_CFileDialog::CFileDialogip
The fourth parameter dwFlag
includes:ci
OFN_HIDEREADONLY:隱藏只讀選項 OFN_OVERWRITEPROMPT:覆蓋已有文件前提 OFN_ALLOWMULTISELECT:容許選擇多個文件 OFN_CREATEPROMPT:若是輸入的文件名不存在,則對話框返回詢問用戶是否根據次文件名建立文件的消息框 OFN_FILEMUSTEXIST:只能輸入已存在的文件名 OFN_FORCESHOWHIDDEN:能夠顯示隱藏的文件 OFN_NOREADONLYRETURN:不返回只讀文件 OFN_OVERWRITEPROMPT:保存的文件已存在時,顯示文件已存在的信息
Other functions, including DoModel, GetPathName, et al. in CFileDialog:get
MSDN_CFileDialog::Other functionsit
As the begining code shows, now we get the file's path name and we restore it into variabla m_filename
, actually, m_filename
is a member variable of the correspoding control ID(We usually generate it in MFC ClassWizard).io
#How to do file processing? MFC provides the CFile class to edit file. Here we take a short code for example:ast
UpdateData(TRUE); if(m_filename=="")return; CFile ff(m_filename,CFile::modeRead); CFile fp(m_savefile,CFile::modeCreate|CFile::modeWrite); fp.SeekToBegin(); ff.SeekToBegin(); ff.Read(inbuff,sizeof(file)); /* operations */ fp.Write(oubuff,sizeof(file)); ...
At last, after we write the file, we should close it:function
ff.Close(); fp.Close();
MSDN gives more details about the explaination of CFile class and its member funcitons:
Tip:
When processing files, we can devide file into small parts and process it respectively. For example, we can take 8 byte from the file everytime:
/* lFileLen = object.GetLength(); */ long c=lFileLen/8; long d=lFileLen%8; for(long i=0;i<c;i++) { ff.Read(inbuff,8); /* operations */ fp.Write(oubuff,8); } if(d>0) { ff.Read(inbuff,d); /* operations */ fp.Write(oubuff,8); }