摘要:ubuntu系统下,C++程序读取指定文件夹中多个文件,保存文件名列表。文件名没有规律且不考虑读取子文件夹中的文件。

系统配置:ubuntu16.04, cmake编译

首先安利一个函数,输入string类型的文件夹路径和vector类型的文件名列表,输出vector类型的文件名列表。

具体可参考链接1链接2

#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <vector>
#include <string> // 如果strcmp()函数报错,则使用<cstring> void GetFileNames(string path,vector<string>& filenames)
{
DIR *pDir;
struct dirent* ptr;
if(!(pDir = opendir(path.c_str()))){
cout<<"Folder doesn't Exist!"<<endl;
return;
}
while((ptr = readdir(pDir))!=) {
if (strcmp(ptr->d_name, ".") != && strcmp(ptr->d_name, "..") != ){
filenames.push_back(path + "/" + ptr->d_name);
}
}
closedir(pDir);
} int main() {
vector<string> file_name;
string path = "/home/gordon/data/slam6d_data/thermolab/pose"; GetFileNames(path, file_name); for(int i = ; i <file_name.size(); i++)
{
cout<<file_name[i]<<endl;
} return ;
}

再次,来介绍一个巨坑 —— io.h !

我一开始采用如下函数,该函数依赖io.h头文件。代码如下,

void getFilesAll(string path, vector<string>& files) {
//文件句柄
long hFile = ;
//文件信息
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -) {
do {
if ((fileinfo.attrib & _A_SUBDIR)) {
if (strcmp(fileinfo.name,".") != && strcmp(fileinfo.name,"..") != ) {
//files.push_back(p.assign(path).append("\\").append(fileinfo.name));
getFilesAll(p.assign(path).append("\\").append(fileinfo.name), files);
}
}
else {
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == );
_findclose(hFile);
}
}

第一次编译后报错:

error: aggregate ‘*********’ has incomplete type and cannot be defined

解决方法: struct _finddata_t fileinfo; 将struct去除, _finddata_t fileinfo;

第二次编译后报错:

error: ‘_finddata_t’ was not declared in this scope

error: ‘_findfirst’ was not declared in this scope

error: ‘_A_SUBDIR’ was not declared in this scope

error: ‘_findnext’ was not declared in this scope

error: ‘_findclose’ was not declared in this scope

尝试过很多方法,包括修改头文件格式,修改CMakeLists.txt的include路径,复制io.h文件到 /usr/include/ 路径下,都失败了。

最后在github的issue上看到有人提到,io.h 头文件可能不兼容跨平台操作。在windows下这个头文件运行稳定,但是在linux下这个头文件不能正常运行。

巨坑呐!!!是真的不兼容吗,还是存在其他问题?求高人指点!

【C++】ubuntu中读取指定目录中的所有文件的更多相关文章

  1. 遍历并读取指定目录下的所有文件内容,写入Map集合然后输出在控制台和本地文件

    public class FileWrite { public static void main(String[] args) throws Exception { //封装数据源目录 File sr ...

  2. Java中读取某个目录下的所有文件和文件夹

    import java.io.File; public class Test1 { public static void main(String[] args) { String path=" ...

  3. linux复制指定目录下的全部文件到另一个目录中

    linux复制指定目录下的全部文件到另一个目录中复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir ...

  4. linux复制指定目录下的全部文件到另一个目录中,linux cp 文件夹

    linux复制指定目录下的全部文件到另一个目录中复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir ...

  5. python中获取指定目录下所有文件名列表的程序

    http://blog.csdn.net/rumswell/article/details/9818001 # -*- coding: utf-8 -*-#~ #------------------- ...

  6. Python —— 批量替换指定目录下的所有文件中指定字符串

    参考:http://blog.csdn.net/zcwfengbingdongguke/article/details/13951527 代码: #!/usr/bin/python import os ...

  7. python glob 用通配符查找指定目录中的文件 - 开源中国社区

    python glob 用通配符查找指定目录中的文件 - 开源中国社区 python glob 用通配符查找指定目录中的文件

  8. java统计指定目录中文件的个数和总的大小

    转: 统计指定目录中文件的个数和总的大小 package file; import java.io.File; import java.util.ArrayList; public class Fil ...

  9. Python3实现从文件中读取指定行的方法

    from:http://www.jb51.net/article/66580.htm 这篇文章主要介绍了Python3实现从文件中读取指定行的方法,涉及Python中linecache模块操作文件的使 ...

随机推荐

  1. MySQL 把两个结果集拼接到一起(两个结果集的列一模一样)

    select * from a UNION all ( select * from b)

  2. .NET并行计算和并发11:并发接口 IProducerConsumerCollection

    using System; using System.Collections; using System.Collections.Generic; using System.Collections.C ...

  3. Ubuntu16.04下ZeroC ICE的安装与使用示例(Qt C++ 和 Java)

    项目需求:在Ubuntu16.04系统下安装并使用ICEgrid 3.7进行c++和Java Springboot开发环境的通信,下面逐一介绍各个步骤的详解: 一:Ice Lib的安装 参考官网地址: ...

  4. GitHub看板系统(Project)

    /********************************************************************** * GitHub看板系统(Project) * 说明: ...

  5. laravel 错误提示Fatal Error: Class 'Pheanstalk\Pheanstalk' not found

    本地环境版本: composer -V Composer version -- ::10 php artisan -V Laravel Framework 5.6.39 错误提示: Fatal Err ...

  6. DoTween之队列

    //引用命名空间 using DG.Tweening; // 初始化一个sequence Sequence sequence = DOTween.Sequence(); //添加动画 sequence ...

  7. E-R视图中有关图形的用法

    这里先推荐几款相对好用的画E-R图的软件: 第一款为:  这是一款在线的画流程图软件 第二款为:亿图图示 第三款为:Visio ER图是在设计数据库之前,明白数据之间的相互关系,理清数据之间的逻辑而需 ...

  8. maven各个属性参数详解

    <project xmlns="http://maven.apache.org/POM/4.0.0 " xmlns:xsi="http://www.w3.org/2 ...

  9. UE4 材质编辑

    1.颜色反转(纹理颜色白变黑,黑变白)

  10. Javascript 4.4

    childNodes属性:可以从给定文档的节点树里把任何一个元素的所有子元素检索出来 返回的值是一个数组,此数组包含给定元素节点的全体子元素:element.childNodes nodeType属性 ...