C++ traverses all files under a file into an array


void FindAllFile(CString path, CString* filenames, int& count)
{
    
    
	CFileFind finder;
	BOOL working = finder.FindFile(path + "\\*.*");
	while (working)
	{
    
    
		working = finder.FindNextFileW();
		if (finder.IsDots())
		{
    
    
			continue;
		}
		else if (finder.IsDirectory())
		{
    
    
			continue;
		}
		else
		{
    
    
			//getfileName 只会得到根文件名, Directory 是包含文件的文件夹
			CString filename = finder.GetFileName();
			std::cout << "file name:" << filename << std::endl;
			filenames[count++] = filename;
		}
	}
}
int main()
{
    
    

	CString filenames[1024];
	int count = 0;
	char path[MAX_PATH];
	while (std::cin >> path)
	{
    
    
		FindAllFile(path, filenames, count);
		for (int i = 0; i < count; i++)
		{
    
    
			//输出一半
			//std::cout << (LPCSTR)(filenames[i].GetBuffer(filenames[i].GetLength())) << std::endl;
			//完整输出目录名子
			std::cout << (CT2A)(filenames[i].GetBuffer(filenames[i].GetLength())) << std::endl;
			
		}
	}


	return 0;
}

Guess you like

Origin blog.csdn.net/guanxunmeng8928/article/details/112284381