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

一、 从路径中 提取扩展名

 

 CString path("C:/ForVcTest/diary.txt"); CString ext = path.Mid(path.ReverseFind('.')+1); 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函数提取出 后缀名

 

二、从路径中 提取文件名

 

CString path("C:/ForVcTest/diary.txt"); CString name = path.Mid(path.ReverseFind('/')+1); AfxMessageBox(name);

 

 

三、获取文件属性

 

DWORD dwAttr = GetFileAttributes("C:/ForVcTest/2.txt");//获取文件的属性 if (dwAttr == FILE_ATTRIBUTE_ARCHIVE){ AfxMessageBox("FILE_ATTRIBUTE_ARCHIVE"); }

 

四、设置文件属性

 

SetFileAttributes("C:/ForVcTest/2.txt",FILE_ATTRIBUTE_READONLY);//|FILE_ATTRIBUTE_HIDDEN

 

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

 

//提取文件路径 char appName[_MAX_PATH]; GetModuleFileName(NULL,appName,_MAX_PATH); CString szPath(appName); AfxMessageBox(szPath);  

 

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

 

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

 

六、移动文件

 

MoveFile("C:/ForVcTest/diary.txt","C:/ForVcTest/newCopy.txt");

 

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

 

七、Path Name Title 的区别

 

CFile file("C:/ForVcTest/newCopy.txt",CFile::modeRead); CString szPath = file.GetFilePath(); CString szName = file.GetFileName(); CString szTitle = file.GetFileTitle(); AfxMessageBox("szPath = "+szPath); AfxMessageBox("szName = "+szName); AfxMessageBox("szTitle = "+szTitle);

 

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

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

szName = "newCopy.txt"

szTitle = "newCopy.txt"

 

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

 

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

 

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

 

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

 

这就是区别吧。

 

 

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

 

八、文件分隔

 

bool SplitFile() { //文件分割 CFile m_File; CString m_FileName,m_FileTitle,m_FilePath; m_FilePath = "C://ForVcTest//newCopy.txt"; char pBuf[40]; if(m_File.Open(m_FilePath,CFile::modeRead | CFile::shareDenyWrite)) { m_FileName=m_File.GetFileName(); m_FileTitle=m_File.GetFileTitle(); // DWORD FileLength=m_File.GetLength(); // DWORD PartLength=FileLength/2+FileLength%2; int nCount=1; CString strName; CFile wrFile; DWORD ReadBytes; while(true) { ReadBytes=m_File.Read(pBuf,40); //ReadBytes 实际读取的字节数 strName.Format("C://ForVcTest//%s%d.txt",m_FileTitle,nCount); wrFile.Open(strName,CFile::modeWrite | CFile::modeCreate); wrFile.Write(pBuf,ReadBytes); wrFile.Close(); if(ReadBytes<40) //实际读取的字节数 不足 分配的大小,则说明文件读完了 break; nCount++; } m_File.Close(); } else{ AfxMessageBox("不能打开文件"); return fasle; } return true; }

转载于:https://www.cnblogs.com/JarvisChu/archive/2010/08/03/2752549.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值