求助C++问题:扫描目录并读取指定的新生成文件到主函数
请教下各位高手,有没有C++编写的扫描目录并读取指定的新生成文件的源代码供菜鸟学习一下。
想实现这样的功能,每隔五分钟扫描一次指定目录,如果有新生成的以QT和XX字母开头的文件(文件后缀为.000),则读取新文件到主函数中,主函数如下:
int main(int argc , char* argv[])
{
if ( argc < 3 )
{
printf( "Paras error.\n" );
return -1;
}
string strFileName = argv[ 1 ];
string strSavePath = argv[ 2 ];
printf( "Current file: %s.\n", strFileName.c_str() );
} C++ 扫描目录 读取指定文件
[解决办法]
正常的遍历文件夹,
然后判断文件的创建时间就可以找到哪些是新生成的了.
[解决办法]
CFileFinder去遍历目录,通过对比字符串信息,你就可以找出相应文件
至于判断新文件,你可以通过时间进行对比。
void GetDirectory(const CString& strDirName)
{
CFileFind theFind;
CString tempFileFind(strDirName + _T("\\*.*"));
BOOL bFinded = theFind.FindFile(tempFileFind);
//遍历文件
while(bFinded)
{
bFinded = theFind.FindNextFile();
if(!theFind.IsDots())
{
CString foundFileName(theFind.GetFileName());
if(theFind.IsDirectory())
{
CString tempDir = strDirName + _T("\\")+foundFileName;
GetDirectory(tempDir);//递归进行子目录查找
}
else
{
CString tempFileName = strDirName + _T("\\")+foundFileName;
gArrFileToZip.Add(tempFileName);//这是你的全部目录文件,你可以修改下去适应你的需要
}
}
}
theFind.Close();
}