巧用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 ...
随机推荐
- 剑指offer--面试题20
题目:从外向里顺时针打印矩阵 做题心得:该题本质上并未考查复杂的数据结构及算法,而是考查了快速找规律的能力!!! 要想作出此题,必须先有绝对清晰的思路,否则越写越乱(因为涉及到很多的循环打印) 自己当 ...
- UIResponder
原网址:http://www.cnblogs.com/kuku/archive/2011/11/12/2246389.html 在 iOS 中,一个 UIResponder 对象表示一个可以接收触摸屏 ...
- 树链剖分 - BZOJ 1036: [ZJOI2008]树的统计Count
这是树链剖分的入门题,也是我学树链剖分的第一题. 树链剖分:就是把树中和线段树联系起来,求(u,v)路径中权值的最大值和其路径的权值和. 入门blog:http://blog.sina.com.cn/ ...
- Mozilla推荐的CSS属性书写顺序及命名规则
传说中的Mozilla推荐 /* mozilla.org Base Styles * maintained by fantasai */ /* Suggested order: * display * ...
- O2O模式成功案例分享 汲取精华化为己用
本文通过分享一些公司的o2o成功案例让您了解什么是O2O,o2o的优势,o2o模式有哪些,未来我们要如何做o2o才更有竞争力,学牛人的o2o创新玩法,摸索适合自己的o2o思路.拥抱o2o - 传统企业 ...
- NET 查找程序集路径(CLR关于Assembly的搜索路径的过程)
最近在回顾.Net应用程序的执行环境,这里做一个很小的总结,方面以后需要的时候进行查找: CLR必须可以找到正确的Assembly,Net提供了Assembly搜索算法,可以根据.config文件(类 ...
- spring mvc Controller与jquery Form表单提交代码demo
1.JSP表单 <% String basePath = request.getScheme() + "://" + request.getServerName() +&qu ...
- Chapter 6面向对象
1.Python中预定义的函数在定义的时候有一种很特别的形式,即是函数名是小写,并且函数名前后分别有两个下划线.同样的,在对象中也有预定义的方法,例如所有对象的基类object中的__new__(), ...
- LA 4287
Consider the following exercise, found in a generic linear algebra textbook. Let A be an n × n matri ...
- PageControl(弹性滚动)
使用网上源码KYAnimatedPageControl self.pageControl = [[KYAnimatedPageControl alloc]initWithFrame:CGRec ...