c++ 获取文件夹下所有文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tszhangjunqiao/article/details/38517577

用c++获取指定文件夹下的所有文件,包括子文件夹里的文件。

void GetAllFiles( string path, list<string>&AllFiles, bool includeSubDir )
{
	long hFile = 0;
	struct _finddata_t fileinfo;
	string p;
	if ( ( hFile = _findfirst( p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
	{
		do
		{
			if ( (fileinfo.attrib & _A_SUBDIR))
			{
				if ( strcmp( fileinfo.name, ".") != 0 && strcmp( fileinfo.name, "..") != 0) //是否为文件夹
				{
					if ( includeSubDir )
					{
						GetAllFiles( p.assign( path).append("\\").append(fileinfo.name), AllFiles, includeSubDir);
					}
				}

			}
			else
			{
				AllFiles.push_back( p.assign(path).append("\\").append(fileinfo.name));
			}
		}while( _findnext( hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
}

函数参数解释:path,指定的文件夹;AllFiles,一个链表保存获取的所有文件; includeSubDir,若为true则去查找子文件夹里的文件,若为false,则不查找子文件夹。



猜你喜欢

转载自blog.csdn.net/tszhangjunqiao/article/details/38517577