ubuntu 系统判断优盘的指定文件存在
有很多的时候 会出现没有用的优盘路径 如果代码中写的是绝对路径 就有可能读不到优盘的内容 ,以下代码就是可以解决这样的问题 我已经封装成一个类
upanpath.h
#ifndef UPANPATH_H
#define UPANPATH_H #include <dirent.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <unistd.h> using namespace std; class UPanPath
{
public:
UPanPath(string file_name,string UPan_name,string target_file_name);
~UPanPath();
string fileName;
string UPanName;
string targetFileName;
vector<string> getFoldernames( const string dir_name, const string filter1);
bool dirIsExit( const string dir_name );
string getFilePath();
};
#define NOROBOT "no find ROBOT file"
#define NOEFFECTIVE "no find effective file"
#endif // UPANPATH_H
upanpath.cpp
#include "upanpath.h" UPanPath::UPanPath(string file_name,string UPan_name,string target_file_name)
{
this->fileName = file_name ;
this->UPanName = UPan_name;
this->targetFileName = target_file_name;
}
/* Get all the files in the folder dir_name , do not show the directories ! */
bool UPanPath::dirIsExit(const string dir_name)
{
// check the parameter !
if( "" == dir_name )
{
cout<<" dir_name is null ! "<<endl;
return false;
}
// check if dir_name is a valid dir
struct stat s;
lstat( dir_name.c_str() , &s );
if( ! S_ISDIR( s.st_mode ) )
{
cout<<dir_name << " is not a valid directory !"<<endl;
return false;
}
return true; }
/* Get all the files in the folder dir_name , do not show the directories !
* 此函数可以得到指定目录下的所有文件和文件夹名,可以对其进行筛选,只要文件,然后再根据两个过滤器依次进行筛选,得到指定的文件。
*/
vector<string> UPanPath::getFoldernames( const string dir_name, const string filter)
{
vector<string> dirs;
// check the parameter !
if( "" == dir_name )
{
cout<<" dir_name is null ! "<<endl;
return dirs;
} // check if dir_name is a valid dir
struct stat s;
lstat( dir_name.c_str() , &s );
if( ! S_ISDIR( s.st_mode ) )
{
cout<<dir_name << " is not a valid directory !"<<endl;
return dirs;
} struct dirent * filename; // return value for readdir()
DIR * dir; // return value for opendir()
dir = opendir( dir_name.c_str() );
if( NULL == dir )
{
cout<<"Can not open directory "<<dir_name<<endl;
return dirs;
}
cout<<"Open the directory successfully!"<<endl; string path;
if( dir_name.substr(dir_name.length()-, ) != "/" )
path = dir_name + "/";
else
path = dir_name; struct stat s_buf;
string dir_tmp;
/* read all the files in the dir ~ */
while( ( filename = readdir(dir) ) != NULL )
{
dir_tmp = filename ->d_name;
// get rid of "." and ".."
if( "." == dir_tmp || ".." == dir_tmp )
continue; /*获取文件信息,把信息放到s_buf中*/
stat( (path + dir_tmp).c_str(), &s_buf );
/*判断是否目录*/
if( !S_ISDIR(s_buf.st_mode) )
continue; if("" != filter)
{
if((int)dir_tmp.find(filter) >= )
dirs.push_back(dir_tmp);
}
else
dirs.push_back(dir_tmp); }
return dirs;
}
string UPanPath::getFilePath()
{
vector<string> dir_f = UPanPath::getFoldernames(UPanPath::fileName,UPanPath::UPanName);
cout << "文件的个数:"<<dir_f.size() << endl;
string faces_dir = "";
vector<string> upan_name ;
for(int i=; i< dir_f.size(); i++)
{
cout << "dir_f[" << i << "]: " << dir_f[i] << endl;
string dir_tmp = UPanPath::fileName; //+"/"+ dir_f[i];
dir_tmp += "/";
dir_tmp += dir_f[i];
dir_tmp += "/";
dir_tmp += UPanPath::targetFileName;
cout << dir_tmp <<endl;
if( != system( ("cd " + dir_tmp).c_str() ) )
{
if( == system( ("sudo rm -rf " + dir_tmp).c_str() ) )
cout << "Delet invalid Upan folder [ " << dir_tmp << " ] successfully..." << endl;
else
cout << "Failed to delete invalid Upan folder [ " << dir_tmp << " ] ..." << endl;
}
else
upan_name.push_back(dir_f[i]);
} int len = upan_name.size();
if ( == len )
{
cout << "Don't find the Upan [ ROBOT ], please insert the Upan first ..." << endl;
return NOROBOT;
} else
{
for (int i=; i<len; i++)
{
string dir_tmp = UPanPath::fileName;
dir_tmp += "/";
dir_tmp += upan_name[i];
dir_tmp += "/";
dir_tmp += UPanPath::targetFileName;
cout << dir_tmp <<endl;
if(UPanPath::dirIsExit(dir_tmp))
{
faces_dir = dir_tmp;
break;
}
}
if("" == faces_dir)
{
cout << "Don't find the folder [ faces ] in the Upan ..." << endl;
return NOEFFECTIVE;
} else
{
cout << "Find the Upan faces folder [ " << faces_dir << " ] ..." << endl;
return faces_dir;
}
}
}
UPanPath::~UPanPath()
{
cout << "析构函数"<< endl;
}
main.cpp
#include <QCoreApplication> #include "upanpath.h" int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv); UPanPath *P = new UPanPath("/media/alvin","ROBOT","faces");
string FilePath = P->getFilePath();
cout << "effective file path :" << FilePath << endl; return a.exec();
}
可以根据函数的返回值的 不同确定什么原因导致读取失败。
ubuntu 系统判断优盘的指定文件存在的更多相关文章
- 服务 在初始化安装时发生异常:System.IO.FileNotFoundException: "file:///D:\testService"未能加载文件或程序集。系统找不到指定文件。
@echo.@if exist "%windir%\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" goto INSTALL ...
- XX.exe 系统找不到指定文件
错误:unable to start ... XX.exe 系统找不到指定文件 今天调试一个项目,关于泊松融合的,项目名叫PoissonEditing,编译通过之后一直再报错,找不到PoissonEd ...
- Could not load file or assembly ‘ Oracle.ManagedDataAccess.EntityFramework, Version=6.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342’ or one of its dependencies系统找不到指定文件 处理方法
前些天做EF Model-First测试,开发环境为VS2013,数据库为Oracle 11g.所有东西都装好数据模型已经建立后准备执行“根据模型生成数据库”命令时,出现:Could not load ...
- 如何解决Visual Studio2010 编译时提示系统找不到指定文件问题
前一段时间,开始使用vs2010编写程序,可是在编译的时候总是报错,提示系统找不到指定文件,导致无法正常运行程序,花了好久时间终于找到原因解决,是因为常规的输出目录 要与链接的常规的输出文件要相对应. ...
- MVC出现错误:系统找不到指定文件(异常来自 HRSULT:0x80070002)
vs2013创建Web应用程序MVC出现错误:系统找不到指定文件(异常来自 HRSULT:0x80070002) 查到博客园VS2013新建Web Application时报错Exception fr ...
- Visual Studio 2013 错误系统找不到指定文件,0x80070002
错误:Visual Studio 2013 按照成功后,可以创建空web项目,但不能建webform 和 mvc 项目. 提示系统找不到指定文件,0x80070002. 解决方式: Step1: Wi ...
- Windows下make clean指令错误[错误码2](系统找不到指定文件)的解决方案
问题来源 因为笔者想用GCC编译器进行Windows下的C语言编程,安装了Mingw-w64的x86_64-posix-seh版本,并按照Visual Studio Code官方的教程,将Mingw- ...
- Could not load file or assembly 'System.ServiceModel.DomainServices.Hosting'.系统找不到指定文件
项目部署到服务器后出现如下错误信息: Parser Error Message: Could not load file or assembly 'System.ServiceModel.Domain ...
- .netCore 反射 :Could not load file or assembly 系统找不到指定文件
“System.IO.FileNotFoundException:“Could not load file or assembly 'ClassLibrary2, Culture=neutral, P ...
随机推荐
- Spark调优_性能调优(一)
总结一下spark的调优方案--性能调优: 一.调节并行度 1.性能上的调优主要注重一下几点: Excutor的数量 每个Excutor所分配的CPU的数量 每个Excutor所能分配的内存量 Dri ...
- c语言学习笔记-break
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 一.break使用中的注意事项 1.break如果用于循环,用来终止循环. 2.break如果用于switch,则用于终止swi ...
- Layui tree 下拉菜单树
1.效果: 2.html 代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
- MySQL InnoDB表和索引之聚簇索引与第二索引
MySQL InnoDB表和索引之聚簇索引与第二索引 By:授客QQ:1033553122 每个InnoDB表都有一个称之为聚簇索引(clustered index)的特殊索引,存储记录行数据.通常, ...
- HBuilder离线打包启用Chrome Inspect调试
解决方法: 修改这个文件 assets/data/dcloud_control.xml <msc version="1.9.9.39354" debug="true ...
- 【PHP调试篇】PHP高性能日志组件SeasLog
简述 什么是SeasLog SeasLog是一个C语言编写的PHP扩展,提供一组规范标准的功能函数,在PHP项目中方便.规范.高效地写日志,以及快速地读取和查询日志. 为什么使用SeasLog 无论在 ...
- 洗礼灵魂,修炼python(13)--模块random,math,pickle
random 1.作用: random模块用于生成随机数 2.常用函数: random:用于生成一个0到1的随机符点数: 0 <= n < 1.0 uniform(a, b):用于生成一个 ...
- Linux进程描述符task_struct结构体详解--Linux进程的管理与调度(一)【转】
Linux内核通过一个被称为进程描述符的task_struct结构体来管理进程,这个结构体包含了一个进程所需的所有信息.它定义在include/linux/sched.h文件中. 谈到task_str ...
- php二维数组去重
php二维数组去重 前言:php一维数组去重很简单,直接array_unique($arr)即可,但是二维数组去重就得自己去写了 二维数组去重方法: /* * 二维数组去重 * 注意:二维数组中的元素 ...
- Django框架的简介
Django框架的背景 Django是一款基于Python开发的全栈式一体化Web 应用框架.2003 年问世之初,它只是 美国一家报社的内部工具,2005 年 7 月使用 BSD 许可证完成了开源. ...