zoukankan      html  css  js  c++  java
  • 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()-1, 1) != "/" )
            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) >= 0)
                    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=0; 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( 0 != system( ("cd " + dir_tmp).c_str() ) )
            {
                if( 0 == 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 ( 0 == len )
        {
            cout << "Don't find the Upan [ ROBOT ], please insert the Upan first ..." << endl;
            return NOROBOT;
        }
    
        else
        {
            for (int i=0; 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();
    }

    可以根据函数的返回值的 不同确定什么原因导致读取失败。

  • 相关阅读:
    第七周作业
    第六周作业
    第五周作业
    第四周作业
    第三周作业
    第二周作业
    第一周作业
    老鼠与盈利
    币值转换
    2015 USP-ICMC gym 100733 J. Summer Wars
  • 原文地址:https://www.cnblogs.com/wanghuixi/p/8923521.html
Copyright © 2011-2022 走看看