WHCSRL 技术网

linux,windows,C++列出路径下文件和文件夹

linux,windows,C++列出路径下文件和文件夹

开源程序说明

本文包含两个C++函数,分别在linux、windows系统下读取指定路径下的文件夹与文件列表。
CSDNgiteegithub的下载路径:
https://download.csdn.net/download/m0_63669222/35866789
https://gitee.com/RaymondXing/print-dir
https://github.com/RaymondXing/printDir
linux程序使用g++编译,windows程序使用VisualStudio编译运行。
经测试,函数适用操作系统:win10,ubuntu 20.04,ubuntu 16.04,raspbian。其余操作系统未经过测试,若程序API使用不当或执行错误还望指教,谢谢。
代码风格是怎么短怎么写,如有需要应重写再加入到各个工程中。
代码仅供参考,需要用时总是忘了怎么写,发网上容易搜到,免得要用时找不到。

linux代码

linux读取程序如下:

//示例程序:输出当前目录下的文件
//编译(compile):g++ printDirLinux.cpp -o printDirLinux -O3 -std=c++11
//运行(execute):./printDirLinux
#include <iostream>
#include <vector>
#include <string>
#include <dirent.h>

//输入文件夹路径dir_name,输出文件夹列表dir与文件列表file
int find_dir_file(std::string dir_name, std::vector<std::string>& dir, std::vector<std::string>& file)
{
	dir.clear();
	file.clear();
	DIR *dirp;
	struct dirent *dp;
	dirp = opendir(dir_name.c_str());
	if(dirp == NULL)
	{
		std::cout << "find_dir_file() error!
 dir_name = " << dir_name << std::endl;
		return -1;
	}
	while ((dp = readdir(dirp)) != NULL)
	{
		if(std::string(dp->d_name) == std::string(".") || std::string(dp->d_name) == std::string(".."))
		{//当前路径或者父路径
			continue;
		}
		else if(dp->d_type == DT_REG)//文件
		{
			file.push_back(std::string(dp->d_name));
		}
		else if(dp->d_type == DT_DIR)//路径
		{
			dir.push_back(std::string(dp->d_name));
		}
	}
	(void) closedir(dirp);
	return 0;
}

int main(int argc, char** argv)
{
	std::vector<std::string> dir, file;
	find_dir_file("./", dir, file);
	for (std::string strOut : dir)
		std::cout << "dir:	" + strOut << std::endl;
	for (std::string strOut : file)
		std::cout << "file:	" + strOut << std::endl;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

其中dirent.d_type用来区分类型,定义在"dirent.h"文件内,ubuntu20.04 LTS系统总计包含9种类型:DT_UNKNOWN(0), DT_FIFO(1), DT_CHR(2), DT_DIR(4), DT_BLK(6), DT_REG(8), DT_LNK(10), DT_SOCK(12), DT_WHT(14)

windows代码

windows读取程序如下:

#include <iostream>
#include <windows.h>
#include <string>
#include <vector>

//注:VisualStudio需要在“配置属性-高级-高级属性-字符集”下设置“使用多字节字符集”而非“使用 Unicode 字符集”。
//注:两种字符集的编码方式和使用的FindFirstFile()函数不同。
//注:使用unicode字符集时需要将(LPCSTR)替换为(LPCWSTR)并将dir_name也转为wchar编码,否则会编译报错或找不到文件。
int find_dir_file(std::string dir_name, std::vector<std::string>& dir, std::vector<std::string>& file)//输入文件夹路径dir_name,输出文件夹列表dir与文件列表file
{
	dir.clear();
	file.clear();
	WIN32_FIND_DATA FindFileData;
	dir_name += "/*.*";
	HANDLE hFind = ::FindFirstFile((LPCSTR)dir_name.c_str(), &FindFileData);
	if (hFind == INVALID_HANDLE_VALUE)
	{
		std::cout << "find_dir_file() error! GetLastError() = " << ::GetLastError() << std::endl;
		std::cout << "dir_name = " << dir_name << std::endl;
		return -1;
	}
	while (1)
	{
		if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)//directory
		{
			dir.push_back(std::string((char*)FindFileData.cFileName));
		}
		else if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)//file
		{
			file.push_back(std::string((char*)FindFileData.cFileName));
		}
		if (!FindNextFile(hFind, &FindFileData))
			break;
	}
	::FindClose(hFind);
	return 0;
}

int main(int argc, char** argv)
{
	std::vector<std::string> dir, file;
	find_dir_file("./", dir, file);
	for (std::string strOut : dir)
		std::cout << "dir:	" + strOut << std::endl;
	for (std::string strOut : file)
		std::cout << "file:	" + strOut << std::endl;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

为便于复制粘贴,将函数直接列出:
linux:
#include
#include
#include
#include <dirent.h>

//输入文件夹路径dir_name,输出文件夹列表dir与文件列表file
int find_dir_file(std::string dir_name, std::vectorstd::string& dir, std::vectorstd::string& file)
{
dir.clear();
file.clear();
DIR *dirp;
struct dirent *dp;
dirp = opendir(dir_name.c_str());
if(dirp == NULL)
{
std::cout << “find_dir_file() error! dir_name = " << dir_name << std::endl;
return -1;
}
while ((dp = readdir(dirp)) != NULL)
{
if(std::string(dp->d_name) == std::string(”.") || std::string(dp->d_name) == std::string("…"))
{//当前路径或者父路径
continue;
}
else if(dp->d_type == DT_REG)//文件
{
file.push_back(std::string(dp->d_name));
}
else if(dp->d_type == DT_DIR)//路径
{
dir.push_back(std::string(dp->d_name));
}
}
(void) closedir(dirp);
return 0;
}
windows:
#include
#include <windows.h>
#include
#include

//注:VisualStudio需要在“配置属性-高级-高级属性-字符集”下设置“使用多字节字符集”而非“使用 Unicode 字符集”。
//注:两种字符集的编码方式和使用的FindFirstFile()函数不同。
//注:使用unicode字符集时需要将(LPCSTR)替换为(LPCWSTR)并将dir_name也转为wchar编码,否则会编译报错或找不到文件。
int find_dir_file(std::string dir_name, std::vectorstd::string& dir, std::vectorstd::string& file)//输入文件夹路径dir_name,输出文件夹列表dir与文件列表file
{
dir.clear();
file.clear();
WIN32_FIND_DATA FindFileData;
dir_name += “/.”;
HANDLE hFind = ::FindFirstFile((LPCSTR)dir_name.c_str(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
std::cout << "find_dir_file() error! GetLastError() = " << ::GetLastError() << std::endl;
std::cout << "dir_name = " << dir_name << std::endl;
return -1;
}
while (1)
{
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)//directory
{
dir.push_back(std::string((char*)FindFileData.cFileName));
}
else if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)//file
{
file.push_back(std::string((char*)FindFileData.cFileName));
}
if (!FindNextFile(hFind, &FindFileData))
break;
}
::FindClose(hFind);
return 0;
}

推荐阅读