VC ++ MFC打开文件对话框

MFC中使用CFileDialog类CFile类完成使用文件对话框读写文件的目的

(1)保存文件对话框

Void CMyDlg::OnFileSave()

{

  CFileDialog *lpszSaveFile;    //定义一个CfileDialog对象
      lpszOpenFile = new CFileDialog(FALSE,””,””,OFN_FILEMUSTEXIST |OFN_HIDEREADONLY , “文件类型(*.txt)|*.txt||”);//生成一个对话框
      if(lpszSaveFile->DoModal()==IDOK)//点击对话框确定按钮,写文件
     {
            CString szGetName;
            szGetName = lpszSaveFile->GetPathName();//得到保存文件的路径

             //打开文件并写入数据
              CFile file(lpszSaveFile->GetFileName(), CFile ::modeCreate | CFile::modeWrite);
              CString str;
              str.Format("%f   %f\r\n ",   35456,   9.6  );
         
             
file.Write(str,strlen(str));
              file.close();
       }
          delete lpszSaveFile;//释放分配的对话框

 

}

 

 

(2)打开文件对话框
Void CMyDlg::OnFileOpen()

{

   CFileDialog *lpszOpenFile;    //定义一个CfileDialog对象
      lpszOpenFile = new CFileDialog(TRUE,””,””,OFN_FILEMUSTEXIST |OFN_HIDEREADONLY , “文件类型(*.txt)|*.txt||”);//生成一个对话框


      if(lpszOpenFile->DoModal()==IDOK)//点击对话框确定按钮,读文件
     {
            CString szGetName;
            szGetName = lpszOpenFile->GetPathName();//得到保存文件的路径
              

             //读取文件
              CFile file(lpszOpenFile->GetFileName(),  CFile::modeRead);
              DWORD dwFileLen=file.GetLength();              //获取文件长度

              char *buf=new char [  dwFileLen +1  ];

              file.Read(buf,         dwFileLen);

              file.close();

             MessageBox(buf);
       }


          delete lpszOpenFile;//释放分配的对话框

 

}

 

  对一些函数的解释:

  CFileDialog(
  BOOL bOpenFileDialog,   //值为TRUE时表示”打开”对话框,值为FALSE时表”保存”对话框
  LPCTSTR lpszDefExt = NULL, //默认的文件名扩张
  LPCTSTR lpszFileName = NULL,
  //打开对话框时默认出现在文本框里的文件名,为NULL时不显示默认文名

  DWORD dwFlags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST
  //对文件属性等定义的组合,

  LPCTSTR lpszFilter = NULL,//可以应用的文件类型,如*.MP3
  CWnd* pParentWnd = NULL,

  // A pointer to the file dialog-box object's parent or owner window.
  DWORD dwSize = 0

  /* The size of the OPENFILENAME structure. This value is dependent on the operating system version, so MFC can determine the appropriate kind of dialog to create (for

example, new Windows 2000 dialogs as opposed to NT4 dialogs). */

  );

 

 

你可能感兴趣的:(File,File,null,null,delete,mfc,mfc,dialog,dialog)