一、 從路徑中 提取副檔名
CString path("C:/ForVcTest/diary.txt");<br />CString ext = path.Mid(path.ReverseFind('.')+1);<br />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");<br />CString name = path.Mid(path.ReverseFind('/')+1);<br />AfxMessageBox(name);
三、擷取檔案屬性
DWORD dwAttr = GetFileAttributes("C:/ForVcTest/2.txt");//擷取檔案的屬性<br />if (dwAttr == FILE_ATTRIBUTE_ARCHIVE){<br />AfxMessageBox("FILE_ATTRIBUTE_ARCHIVE");<br />}
四、設定檔案屬性
SetFileAttributes("C:/ForVcTest/2.txt",FILE_ATTRIBUTE_READONLY);//|FILE_ATTRIBUTE_HIDDEN
五、擷取當前程式所在路徑
//提取檔案路徑<br />char appName[_MAX_PATH];<br />GetModuleFileName(NULL,appName,_MAX_PATH);<br />CString szPath(appName);<br />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);</p><p>CString szPath = file.GetFilePath();<br />CString szName = file.GetFileName();<br />CString szTitle = file.GetFileTitle();</p><p>AfxMessageBox("szPath = "+szPath);<br />AfxMessageBox("szName = "+szName);<br />AfxMessageBox("szTitle = "+szTitle);
我寫了上面一段測試程式,得到的結果是
szPath = "C:/ForVcTest/newCopy.txt"
szName = "newCopy.txt"
szTitle = "newCopy.txt"
MSDN 裡面說 title 是 newCopy 但是我的運行結果和它講的不一樣。
這裡我就不是很明白了,這後兩個概念到底有什麼區別?
我又研究了一番,終於發現了他們的區別。
如果將檔案的尾碼名 隱藏以來,你就發現,name = newCopy.txt 而 title = newCopy
這就是區別吧。
希望看這篇文章的博友能和我一起交流討論這個問題。
八、檔案分隔
bool SplitFile()<br />{<br />//檔案分割<br />CFile m_File;<br />CString m_FileName,m_FileTitle,m_FilePath;<br />m_FilePath = "C://ForVcTest//newCopy.txt";<br />char pBuf[40];<br />if(m_File.Open(m_FilePath,CFile::modeRead | CFile::shareDenyWrite))<br />{<br />m_FileName=m_File.GetFileName();<br />m_FileTitle=m_File.GetFileTitle();<br />//DWORD FileLength=m_File.GetLength();<br />//DWORD PartLength=FileLength/2+FileLength%2;<br />int nCount=1;<br />CString strName;<br />CFile wrFile;<br />DWORD ReadBytes;<br />while(true)<br />{<br />ReadBytes=m_File.Read(pBuf,40); //ReadBytes 實際讀取的位元組數<br />strName.Format("C://ForVcTest//%s%d.txt",m_FileTitle,nCount);<br />wrFile.Open(strName,CFile::modeWrite | CFile::modeCreate);<br />wrFile.Write(pBuf,ReadBytes);<br />wrFile.Close();<br />if(ReadBytes<40) //實際讀取的位元組數 不足 分配的大小,則說明檔案讀完了<br />break;<br />nCount++;<br />}<br />m_File.Close();<br />}<br />else{<br />AfxMessageBox("不能開啟檔案");<br /> return fasle;<br /> }<br /> return true;<br />}<br />