【C++】ubuntu中读取指定目录中的所有文件
摘要:ubuntu系统下,C++程序读取指定文件夹中多个文件,保存文件名列表。文件名没有规律且不考虑读取子文件夹中的文件。
系统配置:ubuntu16.04, cmake编译
首先安利一个函数,输入string类型的文件夹路径和vector类型的文件名列表,输出vector类型的文件名列表。
#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中读取指定目录中的所有文件的更多相关文章
- 遍历并读取指定目录下的所有文件内容,写入Map集合然后输出在控制台和本地文件
public class FileWrite { public static void main(String[] args) throws Exception { //封装数据源目录 File sr ...
- Java中读取某个目录下的所有文件和文件夹
import java.io.File; public class Test1 { public static void main(String[] args) { String path=" ...
- linux复制指定目录下的全部文件到另一个目录中
linux复制指定目录下的全部文件到另一个目录中复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir ...
- linux复制指定目录下的全部文件到另一个目录中,linux cp 文件夹
linux复制指定目录下的全部文件到另一个目录中复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir ...
- python中获取指定目录下所有文件名列表的程序
http://blog.csdn.net/rumswell/article/details/9818001 # -*- coding: utf-8 -*-#~ #------------------- ...
- Python —— 批量替换指定目录下的所有文件中指定字符串
参考:http://blog.csdn.net/zcwfengbingdongguke/article/details/13951527 代码: #!/usr/bin/python import os ...
- python glob 用通配符查找指定目录中的文件 - 开源中国社区
python glob 用通配符查找指定目录中的文件 - 开源中国社区 python glob 用通配符查找指定目录中的文件
- java统计指定目录中文件的个数和总的大小
转: 统计指定目录中文件的个数和总的大小 package file; import java.io.File; import java.util.ArrayList; public class Fil ...
- Python3实现从文件中读取指定行的方法
from:http://www.jb51.net/article/66580.htm 这篇文章主要介绍了Python3实现从文件中读取指定行的方法,涉及Python中linecache模块操作文件的使 ...
随机推荐
- bootstrap-edittable 使用笔记之 (select, data,text, number)
可编辑列表的数据格式可以指定,常用的有select, data, text, number.代码如下. 前端代码: <table id="tb_product" class= ...
- 今天遇到一个关于栈溢出的问题StackOverflowError
关于这个问题个人认为是一个比较棘手的问题,因为我们每个人遇到溢出问题的原因都不一样,所以遇到这样的问题就多从问题的根本入手. 我遇到的原因是,循环多次导致的,以为我的俩个互相关联的实体类,当作查询时, ...
- Java判断字符串是否有重复
检测是否重复: public static boolean checkDifferent(String iniString) { boolean isbool = false; char[] ch ...
- Python 子进程不能input
from threading import Thread from multiprocessing import Process def f1(): name = input('请输入名字') #EO ...
- 关于null的判断
Java中[null]的判断: 1.[null]只能通过is null,is not null判断,任何与的 关系运算(比较,有大于.大于等于.小于.小于等于.等于.不等于六种运算)都是false. ...
- 牛客网PAT乙级(Basic Level)真题-组个最小数 (20)
组个最小数 (20) 时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 给定数字0-9各若干个.你可以以任意顺序排 ...
- Python学习第七课
Python学习第七课 'Alex' "Alex"print('hello'*5) #重复输出字符串 print('hellowold'[2:]) #类似于切片操作:会取出 llo ...
- 最新vue.js完整视频教程12套
0.1智能社vuejs(1-11章全套) 0.2英文版learing vuejs 0.3Vue.js实战小米阅读开发 0.4走进Vue.js2.0 0.5Vuejs教程45节课 0.6Vue.js+N ...
- Python中使用多进程来实现并行处理的方法小结
进程和线程是计算机软件领域里很重要的概念,进程和线程有区别,也有着密切的联系,先来辨析一下这两个概念: 1.定义 进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和 ...
- [LeetCode&Python] Problem 415. Add Strings
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...