【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模块操作文件的使 ...
随机推荐
- python学习第二次笔记
python学习第二次记录 1.格式化输出 name = input('请输入姓名') age = input('请输入年龄') height = input('请输入身高') msg = " ...
- 大数据 - hadoop基础概念 - HDFS
Hadoop之HDFS的概念及用法 1.概念介绍 Hadoop是Apache旗下的一个项目.他由HDFS.MapReduce.Hive.HBase和ZooKeeper等成员组成. HDFS是一个高度容 ...
- 2019微软Power BI 每月功能更新系列——Power BI 4月版本功能完整解读
Power BI4月份的更新对整个产品进行了重大更新.此版本增加了基于DAX表达式定义视觉效果标题和按钮URL的功能.本月Power BI也新增了许多新的连接器,现在可以使用几种预览连接器,包括Pow ...
- windows下用XShell远程控制ubuntu时连接失败
主机和Ubuntu可以相互Ping通,但是XShell远程控制失败. 查看Ip地址方法:ifconfig 解决方案: 1.查看,关闭防火墙状态:sudo ufw disable 2.开放22端口:su ...
- C语言中的一维数组
一.一维数组的定义和引用 (1)一维数组的定义 类型说明符表示数组中的元素类型 数组标识符该数组型变量的名称,命名规则与变量名一致 常量表达式定义了数组中存放的数据元素的个数. (2)一维数组的引用 ...
- 前端导出csv
前端导出csv export: function(data, name) { // csv文件的BOM头 \ufeff可以让excel等识别出csv文件的编码 var uri = 'data:text ...
- B/S与C/S的优缺点
B/S:Browser/Server,即浏览器/服务器架构,一般用于网站: 优点:无需安装,不需要更新客户端: 缺点:交互性差,安全性低. C/S:Client/Server,即客户端/服务器端架构 ...
- C++实验七
#include <iostream> using namespace ::std; int main() { ios_base::fmtflags original_flags = co ...
- Spring(转载二)
在网上看到一篇文章,感觉写得挺不错的,转载一下,本文转载自:http://blog.csdn.net/m13666368773/article/details/7802126 一. IoC理论的背景 ...
- vue day8 table page
@{ ViewBag.Title = "Home Page"; Layout = null; } <!DOCTYPE html> <html> <he ...