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\\";