【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 ...
随机推荐
- Web Services的学习二
1.SOAP简单对象访问协议 基于XML的简单协议,可让应用程序在HTTP上进行信息交换,或者说SOAP就是用于访问网络服务的协议.它独立于平台,独立于语言,很简单并可扩展,而且允许绕过防火墙. 2. ...
- Ubuntu中安装deb包程序
deb是Debian Linux的安装格式,跟Red Hat Linux的rpm非常相似,最基本的安装命令是:dpkg -i file.deb dpkg 是Debian Package的简写,是为De ...
- VisualSVN+TortoiseSVN搭建版本控制系统教程
Tortoise VisualSVN用作SVN的服务端,TortoiseSVN用作SVN的客户端. 一.安装和配置VisualSVN 1.1安装VisualSVN 下载链接:https://www.v ...
- 反向代理/负载均衡/session/cookie
正向代理:客户端将流量重定向到burpsuite等软件或连接到VPN再访问服务器而不是直接访问服务器的场景.流量流动方向是真正机器--代理服务器.正向代理又称代理.普通代理. 反向代理:服务器端使用反 ...
- 牛客网 PAT 算法历年真题 1008 : 锤子剪刀布 (20)
锤子剪刀布 (20) 时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 大家应该都会玩“锤子剪刀布”的游戏:现给出 ...
- python 数字格式化
第二种办法比较常用: %02d print '%02d' % 11
- ButterKnife没有Generate ButterKnife Injections问题
Butterknife 一键自动生成findviewbyid和onclick的代码. 步骤如下: 一: 二: 三: 完成! 如果没有Generate ButterKnife Injections选择项 ...
- UVA 11990 `Dynamic'' Inversion CDQ分治, 归并排序, 树状数组, 尺取法, 三偏序统计 难度: 2
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- unity中导入插件时报错处理办法
错误如下: Unhandled Exception: System.TypeLoadException: Could not load type 'System.ComponentModel.Init ...
- Python之路-python基础一
本章内容: 一.编程语言分类 二.python之变量介绍 三.python交互命令(input,print,getpass) 四.Python之循环控制 ...