MFC 文件及其属性相关操作 (MFC 文件操作 三)

来源:互联网 发布:在哪买淘宝号安全 编辑:程序博客网 时间:2024/06/13 15:18
 

一、 从路径中 提取扩展名

 

 

view plaincopy to clipboardprint?
  1. CString path("C:/ForVcTest/diary.txt");  
  2. CString ext = path.Mid(path.ReverseFind('.')+1);  
  3. AfxMessageBox(ext);  

 

解析:1. CString::Mid

             CString Mid(int nFirst) const;

             CString Mid(int nFirst,int nCount) const;

             nCount代表要提取的字符数,nFirst代表要提取的开始位置

      

         2. CString::CString::ReverseFind

             int ReverseFind( TCHAR ch ) const;

             返回值

                   返回此CString 对象中与要求的字符匹配的最后一个字符的索引;如果没有找 到需要的字符则返回-1。

             参数

                    ch 要搜索的字符

       

         3. 从文件路径中找到 ' . '的位置,然后索引后移一个即是后缀名的起始字符的索引

             利用Mid函数提取出 后缀名

 

二、从路径中 提取文件名

 

view plaincopy to clipboardprint?
  1. CString path("C:/ForVcTest/diary.txt");  
  2. CString name = path.Mid(path.ReverseFind('/')+1);  
  3. AfxMessageBox(name);  

 

 

三、获取文件属性

 

view plaincopy to clipboardprint?
  1. DWORD dwAttr = GetFileAttributes("C:/ForVcTest/2.txt");//获取文件的属性  
  2. if (dwAttr == FILE_ATTRIBUTE_ARCHIVE){  
  3.     AfxMessageBox("FILE_ATTRIBUTE_ARCHIVE");  
  4. }  

 

四、设置文件属性

 

view plaincopy to clipboardprint?
  1. SetFileAttributes("C:/ForVcTest/2.txt",FILE_ATTRIBUTE_READONLY);//|FILE_ATTRIBUTE_HIDDEN  

 

五、获取当前程序所在路径

 

view plaincopy to clipboardprint?
  1. //提取文件路径   
  2. char appName[_MAX_PATH];  
  3. GetModuleFileName(NULL,appName,_MAX_PATH);  
  4. CString szPath(appName);  
  5. AfxMessageBox(szPath);  
  

 

解析:DWORD WINAPI GetModuleFileName(
                        __in_opt HMODULE hModule,
                        __out LPTSTR lpFilename,
                        __in DWORD nSize);

 

        返回包含指定模块的文件的全路径,这个模块必须是已经被当前进程加载的。

 

六、移动文件

 

view plaincopy to clipboardprint?
  1. MoveFile("C:/ForVcTest/diary.txt","C:/ForVcTest/newCopy.txt");  

 

移动后 源文件被删除,目标文件被创建

 

七、Path Name Title 的区别

 

view plaincopy to clipboardprint?
  1. CFile file("C:/ForVcTest/newCopy.txt",CFile::modeRead);  
  2.   
  3. CString szPath = file.GetFilePath();  
  4. CString szName = file.GetFileName();  
  5. CString szTitle = file.GetFileTitle();  
  6.   
  7. AfxMessageBox("szPath = "+szPath);  
  8. AfxMessageBox("szName = "+szName);  
  9. AfxMessageBox("szTitle = "+szTitle);  

 

我写了上面一段测试程序,得到的结果是

szPath = "C:/ForVcTest/newCopy.txt"

szName = "newCopy.txt"

szTitle = "newCopy.txt"

 

MSDN 里面说 title 是 newCopy  但是我的运行结果和它讲的不一样。

 

这里我就不是很明白了,这后两个概念到底有什么区别?

 

我又研究了一番,终于发现了他们的区别。

 

如果将文件的后缀名 隐藏以来,你就发现,name = newCopy.txt  而 title = newCopy

 

这就是区别吧。

 

 

希望看这篇文章的博友能和我一起交流讨论这个问题。

 

八、文件分隔

 

view plaincopy to clipboardprint?
  1. bool SplitFile()   
  2. {  
  3.     //文件分割   
  4.     CFile m_File;  
  5.     CString m_FileName,m_FileTitle,m_FilePath;  
  6.     m_FilePath = "C://ForVcTest//newCopy.txt";  
  7.     char pBuf[40];  
  8.     if(m_File.Open(m_FilePath,CFile::modeRead | CFile::shareDenyWrite))  
  9.     {  
  10.         m_FileName=m_File.GetFileName();  
  11.         m_FileTitle=m_File.GetFileTitle();  
  12. //      DWORD FileLength=m_File.GetLength();  
  13. //      DWORD PartLength=FileLength/2+FileLength%2;  
  14.         int nCount=1;  
  15.         CString strName;  
  16.         CFile wrFile;  
  17.         DWORD ReadBytes;  
  18.         while(true)  
  19.         {  
  20.             ReadBytes=m_File.Read(pBuf,40);   //ReadBytes 实际读取的字节数  
  21.             strName.Format("C://ForVcTest//%s%d.txt",m_FileTitle,nCount);  
  22.             wrFile.Open(strName,CFile::modeWrite | CFile::modeCreate);  
  23.             wrFile.Write(pBuf,ReadBytes);  
  24.             wrFile.Close();  
  25.             if(ReadBytes<40) //实际读取的字节数 不足 分配的大小,则说明文件读完了  
  26.                 break;  
  27.             nCount++;  
  28.         }  
  29.         m_File.Close();  
  30.     }  
  31.     else{  
  32.         AfxMessageBox("不能打开文件");  
  33.                   return fasle;  
  34.           }  
  35.          return true;  
  36. }  

原创粉丝点击