2016/8/16 因为个人科研学习需要,经常需要配合OpenCV进行大量的图像处理和分析,文件遍历,数据输入输出工作。因此自己在公做过程中写了一些常用的类,不怎么精细,也不健壮,但能用,并且会不断改善自己的工具类库。
为了清晰,将整个类分成几块来写
因为经常在些路径的时候忘记最后一个“/”,所以设置了条件选择机制,把漏掉的自动补上。
//设置文件夹路径 //@parameter dir char* 文件夹的路径,如"D:/folder/" bool DirClass::setPath(char* dir) { //如果目录的最后一个字母不是'/',则在最后加上一个'/' int len = strlen(dir); if (dir[len - 1] != '/'){ //char* temp = "/"; char* n_dir = new char[len + 1];//字符数组拼接必须先确定拼接后长度 strcpy(n_dir, dir);//复制字符数组 strcat(n_dir, "/");//拼接字符数组 Path = n_dir;//给绝对路径附正确的值 } else{ Path = dir; } return true; }输入是要遍历的文件类型,输出一个所有该文件类型文件的绝对路径List
//遍历数组返回文件夹内所有文件的绝对路径队列 //@output traverseFile list<string> 遍历指定文件类型的绝对路径列表 //@parameter searchType 要搜索的文件类型 list<string> DirClass::traverseDir(string searchType){ list<string> output;//输出队列 队列适合动态的元素 int ID = 0;//文件计数 //参数准备 int len = strlen(Path); char* dirChar = new char[len + 4]; strcpy(dirChar, Path); strcat(dirChar, "/*.*"); const char* cdirChar = dirChar; //文件遍历结构 _finddata_t fileDir; long lfDir; if ((lfDir = _findfirst(cdirChar, &fileDir)) == -1l) printf("No file is found\n"); else{ //printf("file list:\n"); do{ //printf("%s\n", fileDir.name); String fileName = fileDir.name; int length = fileName.size(); int typeLength = searchType.size(); String fileType = fileName.substr(length - typeLength, length);//获取文件名最后几位,即后缀 //printf("%s\n", fileType); fileType = fileType.toLowerCase(); transform(searchType.begin(), searchType.end(), searchType.begin(), tolower); if (fileType == searchType){ //printf("%s\n", fileName); std::string str = fileName.operator std::string();//CV的String类转std String类 str = Path + str;//组装完整文件路径 output.push_back(str);//放入List队列尾 ID++; } } while (_findnext(lfDir, &fileDir) == 0); } _findclose(lfDir); nfileAmount = ID;//获得文件数目 return output; };计划是可以适应多个类型的文件输出,但实际上我只用txt,其他类型没有写,重载的两个方法,分别用于输出浮点数和字符串
// 写实数到文件夹中指定文件名和文件类型的文件中,如不存在这样的文件可以创建 //@parameter writedata:double 要写入的数字 //@parameter writeFileName: char* 写入目标的文件名,如"file1" //@parameter writeFileType: char* 写入目标的文件类型,如"txt" void DirClass::write(double writedata, char* writeFileName, char* writeFileType){ ofstream outfile; int len = strlen(Path) + strlen(writeFileName) + 1 + strlen(writeFileType);//设置文件完整路径字符串长度 char* fullfilePath = new char[len]; //文件完整路径=文件夹路径+文件名+"."+文件类型 strcpy(fullfilePath, Path); strcat(fullfilePath, writeFileName); strcat(fullfilePath, ".");//strcat字符串拼接函数 strcat(fullfilePath, writeFileType);//strcat函数使用之前必须先设置字符串长度 outfile.open(fullfilePath, ios::in | ios::out | ios::ate | ios::app);//打开文件,关于ofstream::open()函数,详见http://www.cplusplus.com/reference/fstream/ofstream/open/ outfile << writedata ;//输出数字到文件 outfile.close();//关闭文件 } // 写实数到文件夹中指定文件名和文件类型的文件中,如不存在这样的文件可以创建 //@parameter writedata:double 要写入的字符串 //@parameter writeFileName: char* 写入目标的文件名,如"file1" //@parameter writeFileType: char* 写入目标的文件类型,如"txt" void DirClass::write(string writedata, char* writeFileName, char* writeFileType){ ofstream outfile; int len = strlen(Path) + strlen(writeFileName) + 1 + strlen(writeFileType);//设置文件完整路径字符串长度 char* fullfilePath = new char[len]; //文件完整路径=文件夹路径+文件名+"."+文件类型 strcpy(fullfilePath, Path); strcat(fullfilePath, writeFileName); strcat(fullfilePath, ".");//strcat字符串拼接函数 strcat(fullfilePath, writeFileType);//strcat函数使用之前必须先设置字符串长度 outfile.open(fullfilePath, ios::in | ios::out | ios::ate | ios::app);//打开文件 outfile << writedata ;//输出数字到文件 outfile.close();//关闭文件 }如果觉得哪里写的烦琐,实属正常,因为我没系统学过C++,都是靠Baidu一点点摸索出来的。欢迎交流指正。
2017/2/3批注: 这个类是我在折腾OpenCV好一阵子后集结成的常用方法,实际上已经忘了他具体怎么实现,封装好直接用即可。
