读文件夹地址 返回文件路径
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;
}