码迷,mamicode.com
首页 > 系统相关 > 详细

常用函数-Linux文件操作

时间:2019-01-06 13:31:00      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:open   fst   ||   memset   cout   拷贝   格式   path   mem   

 

/************************************************************************
函数功能:寻找文件夹下的某格式文件
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\n",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\n",basePath,ptr->d_name);
   }
   else if (ptr->d_type == 4)    ///dir
   {
    memset(base, \0, 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] = { \0 };
            //获取当前目录绝对路径
            if (NULL == getcwd(path, sizeof(path)))
            {
                printf("***Error***\n");
            }
            app_path = path;
        }
        return app_path;
    }

 

常用函数-Linux文件操作

标签:open   fst   ||   memset   cout   拷贝   格式   path   mem   

原文地址:https://www.cnblogs.com/osbreak/p/10228379.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!