【c++基础】遍历目录并写入txt文件-linux
前言
/******************************************************************************
* File: get_traindata.cpp
* Coder: AMY
* Email: happyamyhope@163.com
* Data: 20181107
*******************************************************************************/
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <fstream> //Global Variables.
char dir[] = "/trainimg/right_eye/";
int const MAX_STR_LEN = ;
std::ofstream traindata;
//Show all files under dir_name, do not show directories.
void showAllFiles(const char* dir_name)
{
std::string path(dir_name);//or path = dir_name;
//check the parameter.
if( NULL==dir_name )
{
std::cout << "dir_name is null..." << std::endl;
return;
}
//check if dir_name is a valid dir.
struct stat s;
lstat(dir_name, &s);
if( !S_ISDIR(s.st_mode) )
{
std::cout << "dir_name is not a valid directory..." << std::endl;
return;
}
struct dirent* filename;//return value for readdir().
DIR* dir;//return value for opendir().
dir = opendir(dir_name);
if( NULL==dir )
{
std::cout << "Can not open dir..." << dir_name << std::endl;
return;
}
std::cout << "Successfully opened the dir..." << std::endl;
//read all the files in the dir.
while( (filename=readdir(dir)) != NULL )
{
//get rid of "." and ".."
if( strcmp(filename->d_name, ".")== || strcmp(filename->d_name, "..")== ) continue;
std::cout << filename->d_name << std::endl;
traindata << path+filename->d_name << std::endl;
traindata << << std::endl;
}
} int main()
{
traindata.open("traindata.txt", std::ios::app);
showAllFiles(dir);
traindata.close();
return ;
}
参考
1.
https://www.itency.com/topic/show.do?id=310867
完
【c++基础】遍历目录并写入txt文件-linux的更多相关文章
- C/C++遍历目录下的所有文件(Windows篇,超详细)
注: 1. 本文讨论的是怎么用Windows API遍历目录下的所有文件.除Windows API,还有一种Windows/Linux通用的方式,使用<io.h>. 2. 本文部分翻译自M ...
- C/C++遍历目录下的所有文件(Windows/Linux篇,超详细)
本文可转载,转载请注明出处:http://www.cnblogs.com/collectionne/p/6815924.html. 前面的一篇文章我们讲了用Windows API遍历一个目录下的所有文 ...
- JAVA读取TXT文件、新建TXT文件、写入TXT文件
1.创建TXT文件 按照正常的逻辑写就好 先定义一个文件给定一个路径——>判断这个路径上这个文件存不存在——>若不存在则建立,try/catch根据程序提示自动生成就好 2.读取TXT文件 ...
- 浅析php fwrite写入txt文件的时候用 \r\n不能换行的问题
以下是对php中fwrite写入txt文件的时候用 \r\n不能换行的问题进行了介绍,需要的朋友可以过来参考下今天遇到了一个问题就是用fwrite写入txt文件的时候用 rn不能换行试了很久都没找到办 ...
- Java读取txt文件和写入txt文件
package com.nickwong.code; import java.io.*; /** * Created by Nickwong on 31/07/2018. * 根据1-8楼的建议,优化 ...
- java追加写入txt文件
整理了下网上的资料,数据追加写入txt文件有三种方式,见下面代码: 方法一: public void method1() { FileWriter fw = null; try { //如果文件存在, ...
- Asp.net读取和写入txt文件方法(实例)!
Asp.NET读取和写入txt文件方法(实例)! [程序第一行的引入命名空间文件 - 参考] System; using System.Collections; using System.Config ...
- Java读取CSV数据并写入txt文件
读取CSV数据并写入txt文件 package com.vfsd; import java.io.BufferedWriter; import java.io.File; import java.io ...
- 生成大量插入语句,并将语句写入txt文件中
import java.io.*; /** * Created by czz on 2019/9/23. */ public class TTest { /** * 生成大量插入语句,并将语句写入tx ...
随机推荐
- 数据标准化+网格搜索+交叉验证+预测(Python)
Download datasets iris_training.csv from:https://github.com/tensorflow/tensorflow/tree/master/tensor ...
- Windows下安装 Memcache
1.下载Memcached Windows服务端程序.(memcached >= 1.4.5 版本安装32 32位系统 1.4.5版本:http://static.runoob.com/down ...
- vue项目如何打包前后端不分离发布手把手教学apache、nginx
vue项目如何不分离发布 1.首先yarn build 我用了vue-cli脚手架,bulid后的dist文件夹里的index.html有加版本号,那么为什么需要加版本号呢? a.回滚 b.解决浏览器 ...
- 如何搭一个vue项目
1.yarn global add @vue/cli (vue/cli是webpack的二次开发) 2.vue create 自定义项目名称 3.选择Manually select featu ...
- mybatis-ResultMappingResolver类信息
ResultMappingResolver类介绍 一个ResultMap元素对应一个ResultMap对象 resultMap元素中的idArg/result/association/collecti ...
- centos 安装 TortoiseSVN svn 客户端
1 安装 svn客户端 yum install -y subversion 2 常用命令操作 检出命令 svn checkout http://svn.com/path
- java倒计时简易实现,只按单线程,以秒为单位
public class Countdown { private int lin; public Countdown(int lin)throws InterruptedException{ this ...
- bzoj2045
题解: 莫比乌斯反演经典题目 直接套公式好了 代码: #include<bits/stdc++.h> using namespace std; ; typedef long long ll ...
- day14-python异常处理
1. 异常 异常即是一个事件,该事件会在程序执行过程中发生,影响了程序的正常执行. 一般情况下,在Python无法正常处理程序时就会发生一个异常.异常是Python对象,表示一个错误.当Pyt ...
- TModalResult 和 MessageBox 返回值
//其实是对应的{ TModalResult values } const mrNone = ; mrOk = idOk; mrCancel = idCancel; mrAbort = idAbort ...