开源程序说明
本文包含两个C++函数,分别在linux、windows系统下读取指定路径下的文件夹与文件列表。
CSDN、gitee和github的下载路径:
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)