巧用ifstream判断文件是否存在
最近在写手写数字的识别软件,训练样例数量巨大而且数字个数不唯一,有可能在中途粘出一部分做测试样例。因此写下面的脚本来获取文件名,之后丢到训练函数里。
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> #include <opencv.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/ml/ml.hpp>
#include <opencv2/core/core.hpp>
#include <opencv_modules.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp> #define cvQueryHistValue_1D( hist, idx0 ) \
((float)cvGetReal1D( (hist)->bins, (idx0))) using namespace std;
using namespace cv; const string rootDir("./sample/");
const string jpg(".jpg");
const string dirNames[] = {
rootDir + "0/", rootDir + "1/", rootDir + "2/",
rootDir + "3/", rootDir + "4/", rootDir + "5/",
rootDir + "6/", rootDir + "7/", rootDir + "8/",
rootDir + "9/"
}; inline void i2s(string& str, int i, int len = ) {
stringstream ss;
ss << setw(len) << setfill('') << i;
str = ss.str();
} void train(string fileName) { } void getFile(string dirName, int num) {
static int sum = ;
string tmp;
string fileName;
string snum;
stringstream ss;
int cur = ; tmp.clear();
ss << num; ss >> snum;
ifstream fileRead;
for (; ; cur++) {
i2s(tmp, cur);
fileName = dirNames[num] + snum + "_" + tmp + jpg;
if (cur == ) cout << "start file name : " << fileName << endl;
fileRead.open(fileName);
if (!fileRead) {
cout << "end file name : " << fileName << endl;
break;
}
fileRead.close();
train(fileName);
}
sum += cur - ;
cout << "current number of samples : " << sum << endl << endl;
} int main() {
int i = ;
for (int i = ; i != ; i++) {
getFile(dirNames[i], i);
}
return ;
}

巧用ifstream判断文件是否存在的更多相关文章
- 转载 - C++ - 关于ifstream/fstream流 判断文件是否结束eof()的问题
出处:http://blog.csdn.net/shuilan0066/article/details/4669451 在做实验的时候遇到这个问题,找原因的时候发现出处除了讲明原因,还举了例子,所以记 ...
- c++ ifstream ofstream 文件流
#include <fstream>ofstream //文件写操作 内存写入存储设备 ifstream //文件读操作,存储设备读区到内存中fstream //读写操作,对打开的文件可进 ...
- C语言中如何判断文件是否存在
方法一:access函数判断文件夹或者文件是否存在 函数原型: int access(const char *filename, int mode); 所属头文件:io.h filename:可以填写 ...
- JavaScript根据文件名判断文件类型
//JavaScript根据文件名判断文件类型 var imgExt = new Array(".png",".jpg",".jpeg",& ...
- PHP判断文件或者目录是否可写
在PHP中,可用is_writable()函数来判断一个 文件/目录 是否可写,详情如下: 参考 is_writable (PHP 4, PHP 5) is_writable — 判断给定的文件名是否 ...
- python os 命令,及判断文件夹是否存在
使用前 import os导入模块 os模块: os.sep 可以取代操作系统特定的路径分割符 os.linesep 字符串给出当前平台使用的行终止符.例如,Windows使用'\r\n ...
- C# 判断文件有没占用
C# 判断文件有没占用 using System; using System.Collections.Generic; using System.Text; using System.Runtime. ...
- Java 判断文件夹、文件是否存在、否则创建文件夹
1.判断文件是否存在,不存在创建文件 File file=new File("C:\\Users\\QPING\\Desktop\\JavaScript\\2.htm"); if( ...
- php 判断文件或目录是否存在
判断文件或目录是否存在有自带的函数 file_exists:文件是否存在 $file = "check.txt"; if(file_exists($file)) { ech ...
随机推荐
- ios 调用打印机
源码 无意中玩一个demo发现调用了打印机 才发现ios有快速调用打印机的功能. if ([UIPrintInteractionController isPrintingAvailable] == ...
- java中byte和blob互转
1. btye[]转blob byte[] bs = ... Blob blob = conn.createBlob(); blob.setBytes(1, bs); ps.setBlob(2, bl ...
- destoon使用中的一些心得
//**************************index首页相关参数**************************************// //全局变量 {if $seo_titl ...
- UML基本表示法(转载)
UML是流行的图解符号.我们都知道,UML是可视化,说明,构建和记录软件和非软件系统的组成部分.这里的可视化是最重要的部分,需要被理解和记忆. UML符号是最重要的建模元素.适当有效地使用符号是非常重 ...
- CSS绝对定位和相对定位 position: absolute/relative
absolute(绝对定位): 会把对象拖离HTML文档流,并通过top, left, right, bottom确定对象的具体位置,这个四个位置属性至少要设置一个,否则无法激活对象的absolute ...
- PHP合并数组array_merge函数运算符加号与的区别
两个的区别是:1.数组键名为数字键名时,要合并的两个数组中有同名数字KEY的时候,使用array_merge()不会覆盖掉原来的值,而使用“+”合并数组则会把最先出现的值作为最终结果返回,而把后面的数 ...
- 7 天玩转 ASP.NET MVC — 第 5 天
目录 第 1 天 第 2 天 第 3 天 第 4 天 第 5 天 第 6 天 第 7 天 0. 前言 欢迎来到第五天的学习.希望第一天到第四天的学习,你都是开心的. 1. Lab 22 - 增加 Fo ...
- Libevent详细说明
文章来自官方文档的部分翻译:http://www.wangafu.net/~nickm/libevent-book/ 通过这部分的了解,基本上可以使用libevent的常用功能了.有时间建议直接看官方 ...
- Eclipse 字体选择
Windows下推荐使用Consolas Linux下推荐使用DejaVu Sans Mono, Website: http://dejavu-fonts.org/wiki/Main_PageDown ...
- mvc5 @RenderSection("scripts", required: false) 什么意思
在模板中 相当于占位符 使用方法如下 @section scripts{ //coding }