使用C++獲取文件夾下的所有文件

Getting a list of files in a directory using C++ in windows
這裏搬運過來的
搜了好多CSDN上面的,都是複製來複制去(我的也是複製到的,哈哈),總是報錯
。。。

#include <string>
#include <vector>
#include <iostream>

using namespace std;


void getDir(const char* d, vector<string> & f)
{
	FILE* pipe =  NULL;
	string pCmd = "dir /B /S " + string(d);
	char buf[256];

	if( NULL == (pipe = _popen(pCmd.c_str(),"rt")))
	{
		cout<<"Shit"<<endl;
		return;
	}

	while (!feof(pipe))
	{
		if(fgets(buf,256,pipe) != NULL)
		{
			f.push_back(string(buf));
		}

	}

	_pclose(pipe);


}

int main(int argc, char* argv[])
{
	vector<string> files;

	getDir("C:\\", files);


	vector<string>::const_iterator it = files.begin();
	cout<<"Printing Dir"<<endl;

	while( it != files.end())
	{
		cout<<*it<<endl;
		it++;
	}
	return 0;
	
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章