【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模块操作文件的使 ...
随机推荐
- makefile笔记4 - makefile命令
每条规则中的命令和操作系统 Shell 的命令行是一致的. make 会一按顺序一条一条的执行命令,每条命令的开头必须以[Tab]键开头,除非,命令是紧跟在依赖规则后面的分号后的. 在命令行之间中的空 ...
- c#计算器
代码没有大的问题,但是起初点击控件无反应,原因是事件代码要自己敲,不能直接粘贴. using System;using System.Collections.Generic;using System. ...
- 【原创】Proxmark3系列教程1——PM3用法
1 PM3介绍 proxmark3是一款开源的RFID安全研究平台黑色按钮从图中我们可以看到左上方有一颗黑色按钮,这个按钮就是Proxmark3的功能键,主要用于启动嗅探模式以及停止进程功能,其中内置 ...
- python—DAY1
# user = "123"# possword = "111"# count = 0## while count < 3:# user_name = i ...
- bind啊你返回的函数到底是个虾米
一般apply().call()和bind()会一起比较. 他们三个都是改变this对象指向的方法. apply()和cal()方法是会立即执行,apply把参数作为数组,call方法接收一个一个的参 ...
- C语言---指针变量详解2
指针变量保存的是地址,本质上是一个整数,可以进行部分运算,例如加法.减法.比较等,请看下面的代码: #include <stdio.h> int main(){ int a = 10, * ...
- [转]How to Send Ethereum with Web3.js and Node
原文:https://davekiss.com/ethereum-web3-node-tutorial/ Ethereum took the web and cryptocurrency worl ...
- 3.7 unittest之断言
3.7 unittest之断言 前言在测试用例中,执行完测试用例后,最后一步是判断测试结果是pass还是fail,自动化测试脚本里面一般把这种生成测试结果的方法称为断言(assert).用unitte ...
- C#创建、读写、增加、删除XML操作
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- LeetCode第一次刷题
感觉自身编程水平还是差很多,所以刷刷题 LeetCode貌似是一个用的人比较多的题库,下面是第一题 给数组和目标和求需要元素的下标 public class Solution { public int ...