有很多的时候 会出现没有用的优盘路径 如果代码中写的是绝对路径 就有可能读不到优盘的内容 ,以下代码就是可以解决这样的问题  我已经封装成一个类

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 系统判断优盘的指定文件存在的更多相关文章

  1. 服务 在初始化安装时发生异常:System.IO.FileNotFoundException: "file:///D:\testService"未能加载文件或程序集。系统找不到指定文件。

    @echo.@if exist "%windir%\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" goto INSTALL ...

  2. XX.exe 系统找不到指定文件

    错误:unable to start ... XX.exe 系统找不到指定文件 今天调试一个项目,关于泊松融合的,项目名叫PoissonEditing,编译通过之后一直再报错,找不到PoissonEd ...

  3. 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 ...

  4. 如何解决Visual Studio2010 编译时提示系统找不到指定文件问题

    前一段时间,开始使用vs2010编写程序,可是在编译的时候总是报错,提示系统找不到指定文件,导致无法正常运行程序,花了好久时间终于找到原因解决,是因为常规的输出目录 要与链接的常规的输出文件要相对应. ...

  5. MVC出现错误:系统找不到指定文件(异常来自 HRSULT:0x80070002)

    vs2013创建Web应用程序MVC出现错误:系统找不到指定文件(异常来自 HRSULT:0x80070002) 查到博客园VS2013新建Web Application时报错Exception fr ...

  6. Visual Studio 2013 错误系统找不到指定文件,0x80070002

    错误:Visual Studio 2013 按照成功后,可以创建空web项目,但不能建webform 和 mvc 项目. 提示系统找不到指定文件,0x80070002. 解决方式: Step1: Wi ...

  7. Windows下make clean指令错误[错误码2](系统找不到指定文件)的解决方案

    问题来源 因为笔者想用GCC编译器进行Windows下的C语言编程,安装了Mingw-w64的x86_64-posix-seh版本,并按照Visual Studio Code官方的教程,将Mingw- ...

  8. Could not load file or assembly 'System.ServiceModel.DomainServices.Hosting'.系统找不到指定文件

    项目部署到服务器后出现如下错误信息: Parser Error Message: Could not load file or assembly 'System.ServiceModel.Domain ...

  9. .netCore 反射 :Could not load file or assembly 系统找不到指定文件

    “System.IO.FileNotFoundException:“Could not load file or assembly 'ClassLibrary2, Culture=neutral, P ...

随机推荐

  1. Nginx 500错误总结

    Nginx 500错误总结 500(服务器内部错误) 服务器遇到错误,无法完成请求. 501(尚未实施) 服务器不具备完成请求的功能.例如,当服务器无法识别请求方法时,服务器可能会返回此代码. 502 ...

  2. vue常用笔记

    vue源码解析(勾三股四):http://jiongks.name/blog/vue-code-review/ 0.npm: https://www.npmjs.com/ 1. package.jso ...

  3. Docker 容器备份例子

    # 在 node1 执行 nginx 程序,挂载本地的目录 docker pull nginx:stable-alpine mkdir /data/html echo "hello worl ...

  4. 洛谷P4007 小 Y 和恐怖的奴隶主(期望dp 矩阵乘法)

    题意 题目链接 Sol 首先不难想到一种暴力dp,设\(f[i][a][b][c]\)表示还有\(i\)轮没打,场上有\(a\)个1血,\(b\)个2血,\(c\)个三血 发现状态数只有\(s = 1 ...

  5. mybatis 中between and用法

    今天遇到一个问题,半天没看出来问题,特意记录一下 Dao ConfigEvaluation findConfigEvaluationByEvalpecent(BigDecimal evalPercen ...

  6. JAVA 和.NET在安全功能的比较

    以下转载于: http://www.it28.cn/ASPNET/825159.html 本文根据Denis Piliptchouk的文章翻译.摘录而来,有些术语翻译不太好理解,还请参考原文. 第一部 ...

  7. Android学习笔记----Java中的字符串比较

    用习惯了C#.C++,在做字符串比较时想当然地使用如下语句: string str1 = "abcd", str2 = "abcd"; if(str1==str ...

  8. Expo大作战(四十一)【完】--expo sdk 之 Assets,BarCodeScanner,AppLoading

    Expo大作战系列完结! 简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与 ...

  9. Centos7安装搭建Bugzilla 5.0

    1.安装准备: Centos7保证网络连通,如果网络不能连通,可通过配置yum源使用代理服务. vim /etc/yum.conf # The proxy server - proxy server: ...

  10. RabbitMQ延迟消息学习

    准备做一个禁言自动解除的功能,立马想到了订单的超时自动解除,刚好最近在看RabbitMQ的实现,于是想用它实现,查询了相关文档发现确实可以实现,动手编写了这篇短文. 准备工作 1.Erlang安装请参 ...