2.獲取路徑並順序存儲文件
a.得到當前運行程序所在路徑(其實得到的是當前執行程序存放路徑)(考察!!!)
TCHAR szFilePath[MAX_PATH + 1];
GetModuleFileName(NULL, szFilePath, MAX_PATH);
(_tcsrchr(szFilePath, _T('//')))[1] = 0;
CString strtemp=szFilePath;
函數說明:
GetModuleFileName:The GetModuleFileName function retrieves the full path and filename for the executable file containing the specified module.
b.得到程序當前工作路徑:(因爲程序在運行過程中,會改變工作路徑)
char pBuf[MAX_PATH]; //存放路徑的變量
GetCurrentDirectory(MAX_PATH,pBuf); //獲取程序的當前目錄
strcat(pBuf,"//");
CString strtemp=pBuf;
函數說明:
GetCurrentDirectory:The GetCurrentDirectory function retrieves the current directory for the current process
c.通過路徑得到目錄並自動生成文件名順序存取文件
例如:CString m_BmpPath中保存當前文件的路徑,我們要得到該文件所在的文件夾路徑也就是目錄。則需進行以下操作:
int n = m_BmpPath.ReverseFind('//');//查找最右側‘/’的位置
m_BmpPath = m_BmpPath.Left(n);//截取最右側‘/’左側部分
m_BmpPath += '//';//添加‘/’
使用int nFrameNo 進行計數並將其作爲文件名,將文件存放在m_BmpPath目錄下,進行以下操作:
CString strFilePath;//定義文件路徑存放使用的字符串
for(;;)
{
strFileName.Format("%s%d.bmp",strBmpPath,lFrameNo);//動態生成文件名
//保存文件
}
d.指定路徑並順序讀取該路徑下的文件
假定在文件夾D:/File/ 下存放有100幅圖片,圖片命名是有規律的,我們要讀取這些圖片。則需進行以下操作:
CFileFind finder; //定義CFileFind對象
CString strName; //保存文件路徑名
BOOL bIsWorking; //標誌是否找完所有文件
SetCurrentDirectory("D://File");//設置查找目錄
bIsWorking = finder.FindFile("*.bmp");//開始一個查找,查找.bmp文件
for ( ;; )//循環查找 注:FindNextFile()找到最後一個文件時返回0;
{
if(bIsWorking)
{
bIsWorking = finder.FindNextFile();
strName = finder.GetFilePath();//文件路徑
讀取文件;
}
else
break;
}
TCHAR CurDir[MAX_PATH];
GetCurrentDirectory(MAX_PATH,CurDir);
CString m_mark1=CurDir;
m_mark1+=*(CurDir+2);
m_mark1+="Markmod";
m_mark1+=*(CurDir+2);
m_mark1+="Mark1.bmp";
結果爲: 文件路徑//Markmod//Mark1.bmp
獲取執行文件夾路徑:
char szFilePath[100];
GetModuleFileName(NULL, szFilePath, MAX_PATH);
CString m_filepath =CString(szFilePath);
int len=m_filepath.GetLength();
int i=0;
for (i=len-1;i>-1;i--)
{
if(m_filepath[i]!= '\\')
m_filepath.Delete(i,1); //去除執行文件名
else
break;
}
m_filepath += "Image1\\";