C++读文件夹 文件

读文件夹地址 返回文件路径
https://www.cnblogs.com/gdut-gordon/p/9678175.html

std::vector<std::string> FilterPath::getFilesList(std::string path) {
  std::vector<std::string> filenames;
  DIR *pDir;
  struct dirent *ptr;
  if (!(pDir = opendir(path.c_str()))) {
    std::cout << "Folder doesn't Exist!" << std::endl;
    return filenames;
  }
  while ((ptr = readdir(pDir)) != 0) {
    if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) {
      // filenames.push_back(path + "/" + ptr->d_name);    //获取文件绝对路径
      filenames.push_back(ptr->d_name);  //只获取文件名
    }
  }
  closedir(pDir);
  return filenames;
}
#include <dirent.h>
#include <sys/types.h>
#include <algorithm>
#include <iostream>
#include <vector>

读文件地址 返回文件内容
输入地址只能是文件的绝对地址或者在当前文件夹下运行输入地址是文件名

//获取当前程序运行所在绝对地址:
  char buff[250];
  getcwd(buff, 250);
  std::string current_path(buff);
  //或者下面这个方法
 #include <unistd.h>
#include <iostream>
using namespace std;
int main(){
    //char *path;
    string path;
    path = get_current_dir_name();
    cout << "path:" << path <<endl;
    return 0;
}
std::string OpValidation::ReadContent(std::string file) {
  std::ifstream infile;
  infile.open(file.data());
  assert(infile.is_open());
  std::string pre = "";
  std::string s;
  while (getline(infile, s)) {
    pre += s + "\n";
  }
  infile.close();
  return pre;
}
#include <sys/stat.h>
#include <unistd.h>
#include <cassert>
#include <fstream>
#include <iostream>
#include <memory>
#include <vector>

向文本最后追加字符串

判断是否为文件

#include <iostream>
#include <fstream>

  struct stat buffer;
  bool isfile = (stat(input_.c_str(), &buffer) == 0 && S_ISREG(buffer.st_mode));
  int result;

  if (isfile) { } // if it is a file run the code

判断是否为文件


#include <iostream>
#include <fstream>
#include <string.h>
  std::string path_last = strrchr(output_.c_str(), '/');
int pos = path_last.find(".");
 if (pos==-1) {

创建文件

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    ofstream oFile;
    //不存在则新建文件
    oFile.open("test2.html");

}

创建文件夹

#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
#include <memory>
void RecursivelyCreateDir(const char *dir) {
  char tmp[PATH_MAX];
  char *p = NULL;
  size_t len;
  snprintf(tmp, sizeof(tmp), "%s", dir);
  len = strlen(tmp);
  if (tmp[len - 1] == '/') {
    tmp[len - 1] = 0;
  }
  // if dir is exist, skip
  if (access(tmp, 0) == 0) {
    return;
  }
  for (p = tmp + 1; *p; p++) {
    if (*p == '/') {
      *p = 0;
      mkdir(tmp, S_IRWXU);
      *p = '/';
    }
  }
  mkdir(tmp, S_IRWXU);
}

读取文件

// read file content
std::string OpValidation::ReadContent(std::string file) {
  std::ifstream infile;
  infile.open(file.data());
  assert(infile.is_open());
  std::string pre = "";
  std::string s;
  while (getline(infile, s)) {
    pre += s + "\n";
  }
  infile.close();
  return pre;
}

把输入的相对路径转换成绝对路径

#include <dirent.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
// If it's a relative path, convert it to an absolute path
std::string FilterPath::GetAbsPath(std::string dir) {
  std::string abs_path = "";
  std::string current_path = get_current_dir_name();
  if (dir.substr(0, 1) == "/") {
    abs_path = dir;
  } else {
    abs_path = current_path + "/" + dir;
  }
  return abs_path;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值