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

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. Mybatis事务管理

    一.Mybatis事务 1.事务管理方式 Mybatis中的事务管理方式有两种: 1.JDBC的事务管理机制,即使用JDBC事务管理机制进行事务管理 2.MANAGED的事务管理机制,Mybatis没 ...

  2. design mode(php)

    前段时间看了下设计模式 参考,以及head first设计模式,简要如下 ## OO原则 * 封装变化* 多用组合,少用继承* 针对接口编程,不针对实现编程* 为交互对象之间的松耦合设计而努力* 开闭 ...

  3. 拜小白教你OpenCV3.2.0+VS2017开发环境配置

    第一部分:OpenCV3.2.0的下载 OpenCV官方下载地址: http://opencv.org/releases.html# 本人选择opencv3.2.0基于Windows平台.读者根据自己 ...

  4. JS如何判断一个数组是否为空、是否含有某个值

    一.js判断数组是否为空 方法一:  arr.length let arr = []; if (arr.length == 0){ console.log("数组为空") }els ...

  5. MySQL and Sql Server:Getting metadata using sql script (SQL-92 standard)

    MySQL: use sakila; -- show fields from table_name; -- show keys from table_name; SELECT `REFERENCED_ ...

  6. csharp: Configuring ASP.NET with Spring.NET and FluentNHibernate

    Domain: FluentNhibernateLocalSessionFactoryObject.cs using System; using System.Collections.Generic; ...

  7. Linux 为linux enterprises 6安装图形桌面教程

    为linux enterprises 6安装图形桌面教程 by:授客 QQ:1033553122 安装系统后发现没图形界面,安装Xwindow[为了避免权限不足,以root登录] 步骤1.启动图形界面 ...

  8. Git服务器配置及本地克隆提交、服务器获取

    1.服务器Git安装配置 相关链接 相关链接 注意ssh-keygen .修改权限 权限:    相关链接   2.本地获取 git clone name@ip:服务器项目位置 相关链接   3.创建 ...

  9. Ubuntu18.04 安装mysql8.0.11

    1.下载deb包 2.运行命令 sudo dpkg -i mysql-apt-config_0.8.10-1_all.deb sudo apt update 3.安装mysql8 sudo apt i ...

  10. @RequestMapping --注解用法详解

    一.编码时我们总会发现如下变量未被使用的警告提示: 上述代码编译通过且可以运行,但每行前面的“感叹号”就严重阻碍了我们判断该行是否设置的断点了.这时我们可以在方法前添加 @SuppressWarnin ...