zoukankan      html  css  js  c++  java
  • 常用函数-Linux文件操作

    /************************************************************************
    函数功能:寻找文件夹下的某格式文件
    std::vector<string> &filelist            -- 文件名list
    const char *basePath         -- 文件路径
    string format             -- 文件格式 如 .xml
    ************************************************************************/
    int readFileList(std::vector<string> &filelist, const char *basePath, string format)
     {
      DIR *dir;
      struct dirent *ptr;
      char base[1000];
      if ((dir = opendir(basePath)) == NULL)
      {
       perror("Open dir error...");
       exit(1);
      }
      while ((ptr = readdir(dir)) != NULL)
      {
       if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)    ///current dir OR parrent dir
        continue;
       else if (ptr->d_type == 8)    //file
       {
        //printf("d_name:%s/%s
    ",basePath,ptr->d_name);
        string temp = ptr->d_name;
        //cout  << temp << endl;
        string sub = temp.substr(temp.length() - 4, temp.length() - 1);
        //cout  << sub << endl;
        if (sub == format)
        {
         //string path = basePath;
         //path += "/";
         //path += ptr->d_name;
         filelist.push_back(ptr->d_name);
        }
       }
       else if (ptr->d_type == 10)    ///link file
       {
        //printf("d_name:%s/%s
    ",basePath,ptr->d_name);
       }
       else if (ptr->d_type == 4)    ///dir
       {
        memset(base, '', sizeof(base));
        strcpy(base, basePath);
        strcat(base, "/");
        strcat(base, ptr->d_name);
        readFileList(filelist, base, format);
       }
      }
      closedir(dir);
      return 1;
     }
    /************************************************************************
    函数功能:文件夹内文件拷贝到另一文件夹
    std::vector<string> &filelist             -- 文件名list
    std::string& srcDirPath        -- 源路径
    std::string& desDirPath          -- 目的路径
    ************************************************************************/
     void do_copy(const std::vector<std::string> &fileNameList, std::string& srcDirPath, std::string& desDirPath)
     {
      for (int i = 0; i < fileNameList.size(); i++)
      {
       std::string nowSrcFilePath, nowDesFilePath;
       nowSrcFilePath = srcDirPath + "/" + fileNameList.at(i);
       nowDesFilePath = desDirPath + "/" + fileNameList.at(i);
       std::ifstream in;
       in.open(nowSrcFilePath);
       if (!in)
       {
        std::cout << "open src file : " << nowSrcFilePath << " failed" << std::endl;
        continue;
       }
       std::ofstream out;
       out.open(nowDesFilePath);
       if (!out)
       {
        std::cout << "create new file : " << nowDesFilePath << " failed" << std::endl;
        in.close();
        continue;
       }
       out << in.rdbuf();
       out.close();
       in.close();
      }
     }
    /************************************************************************
    函数功能:删除某路径下的文件
    std::vector<string> &filelist             -- 文件名list
    std::string& srcDirPath        -- 路径
    ************************************************************************/
     void do_delete(const std::vector<std::string> &fileNameList, std::string& srcDirPath)
     {
      for (int i = 0; i < fileNameList.size(); i++)
      {
       std::string nowSrcFilePath;
       nowSrcFilePath = srcDirPath + "/" + fileNameList.at(i);
       remove(nowSrcFilePath.c_str());
      }
     }
    //*************************************************************************
        // 函数名称: get_app_path
        // 返 回 值: const std::string&
        // 参    数: const char * init
        // 函数说明: 可以把main函数的第一个参数传进init,以它的路径初始化app_path
        // 但要注意init参数在第一次调用get_app_path时有效,否则无效
        //*************************************************************************
        const std::string& get_app_path(const char* init)
        {
            static std::string app_path;
            if (app_path.empty())
            {
                if (NULL != init)
                {
                    app_path = init;
                    return app_path;
                }
    
                char path[128] = { '' };
                //获取当前目录绝对路径
                if (NULL == getcwd(path, sizeof(path)))
                {
                    printf("***Error***
    ");
                }
                app_path = path;
            }
            return app_path;
        }
  • 相关阅读:
    ABAP TCode
    SAP 常用的事务代码
    SAP FI TCode
    Little Tutorials的一篇文章断言:UML(统一建模语言)正在死亡:
    SAP PP TCode
    [ZT]解密中国IT人十大职业现状
    User Exit Query
    SAP客户端多语言设置
    一个女CIO的诞生
    DIY防奸手册之 主流硬盘型号解惑篇
  • 原文地址:https://www.cnblogs.com/osbreak/p/10228379.html
Copyright © 2011-2022 走看看