c_cpp C ++目录列表器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp C ++目录列表器相关的知识,希望对你有一定的参考价值。
#include <stdio.h>
#include <string>
#include <iostream>
#include <windows.h>
#include <vector>
using namespace std;
//Simple struct to return from lsfiles
struct List {
vector<string> files;
vector<string> folders;
};
//All of the hard work
struct List lsfiles(string folder) //(c) http://stackoverflow.com/a/20847429/1009816
{
vector<string> files; //Will be added to List
vector<string> folders; //Will be added to List
char search_path[200];
sprintf(search_path, "%s*.*", folder.c_str());
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile(search_path, &fd);
if(hFind != INVALID_HANDLE_VALUE)
{
do
{
// read all (real) files in current folder, delete '!' read other 2 default folder . and ..
if(! (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
{
files.push_back(fd.cFileName);
} else //Put folders into vector
{
folders.push_back(fd.cFileName);
}
}while(::FindNextFile(hFind, &fd));
::FindClose(hFind);
}
List me;
me.files = files;
me.folders = folders;
return me;
}
int main(int argc, char *argv[])
{
string where;
if(argc > 1){where = argv[1] + string("\\");}else{where = "";}
List you = lsfiles(where); //Get contents of directory
vector<string>::iterator files_begin = you.files.begin();
vector<string>::iterator files_end = you.files.end();
vector<string>::iterator folders_begin = you.folders.begin();
vector<string>::iterator folders_end = you.folders.end();
for(; folders_begin != folders_end; folders_begin++){
cout << "[D] " << *folders_begin << "\n";
}
for(; files_begin != files_end; files_begin++){
cout << "[F] " << *files_begin << "\n";
}
return 0;
}
/////////Example output
//PS C:\> mls
//
//[D] $Recycle.Bin
//[D] adobeTemp
//[D] AMD
//[D] android-sdk
//[D] apache-ant-1.9.3
//[D] Documents and Settings
//[D] FWBuilder51
//[D] inetpub
//[D] iSCSIVirtualDisks
//[D] Media
//[D] MinGW-4.8.1
//[D] MSOCache
//[D] PerfLogs
//[D] Program Files
//[D] Program Files (x86)
//[D] ProgramData
//[D] Python27
//[D] Sandbox
//[D] SWTOOLS
//[D] System Volume Information
//[D] TFTP-Root
//[D] UnitTest++-1.3
//[D] Users
//[D] vz
//[D] Windows
//[F] bootmgr
//[F] BOOTNXT
//[F] pagefile.sys
//PS C:\> mls AMD
//[D] .
//[D] ..
//[D] AMD_Catalyst_12.6_Legacy_Win8
//PS C:\>
以上是关于c_cpp C ++目录列表器的主要内容,如果未能解决你的问题,请参考以下文章
c_cpp 文件管理,用于获取某一目录下的所有文件名称等.FileBrower用于获取文件夹下的名称列表
[C/C++]详解STL容器8-mapmultimapsetmultiset的介绍和使用
[C/C++]详解STL容器8-mapmultimapsetmultiset的介绍和使用
[C/C++]详解STL容器8-mapmultimapsetmultiset的介绍和使用
c_cpp Ç遍历,创建指定目录下的文件.C
C预处理器和C库--1