C++中的文件和文件夹操作
File and Folder Operations in C++
我正在用C++编写一个todo列表管理器程序,并希望执行以下操作:
- 检查程序的工作目录中是否存在目录,如果不创建该目录
- 如果它确实存在,请从中获取.txt文件列表
- 能够从此目录创建/删除.txt文件
我尝试过使用boost/filesystem.hpp
,但似乎不明白(或者如何使用g++将其链接)。下面是我尝试过的一个例子(假设正确的#includes
、int main
等):
std::vector<std::string> findLists(void){
std::vector<std::string> lists;
std::string temp;
char dir[ MAX_PATH ];
std::string(dir, GetModuleFileName(NULL, dir, MAX_PATH));
dir = dir.substr(0,dir.find_last_of( "\/" ));
path p(dir);
for(auto i = directory_iterator(p); i != directory_iterator(); i++){
if(!is_directory(i->path())){
temp = i->path().filename().string();
if(temp.compare(0,temp.find(".")+1,".txt")){
temp = temp.substr(0,temp.find("."));
}
lists.push_back(temp);
}
else{
continue;
}
}
return lists;
}
来自Boost文档
int main(int argc, char* argv[])
{
path p (argv[1]); // p reads clearer than argv[1] in the following code
if (exists(p)) // does p actually exist?
{
if (is_regular_file(p)) // is p a regular file?
cout << p << " size is " << file_size(p) << 'n';
else if (is_directory(p)) // is p a directory?
cout << p << "is a directoryn";
else
cout << p << "exists, but is neither a regular file nor a directoryn";
}
else
cout << p << "does not existn";
return 0;
}
那里有你需要的所有设施。这只是一个起点,但你应该能够很快度过难关。
相关文章:
- 如何将更多文件夹添加到c++include路径
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- VS2017,C++包含目录与附加包含目录,子文件夹包含失败-但为什么
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 如何在文档文件夹中创建目录
- "assimp/config.h"找不到,但它在文件夹中
- C++入门 5 版:类消息和文件夹
- 我可以在 Arduino 的库文件夹之外安装库吗?
- 如何按文件扩展名引用文件夹中的文件
- 在QTreeView中仅显示共享驱动器和文件夹
- 加载与引用 .NET DLL 位于同一文件夹中的引用的 .NET DLL 时"Not found"异常
- 介子复制/安装头文件到输出目录并保持文件夹结构
- vcruntime.h 从 Windows 包含文件夹中丢失?
- 在文件夹迭代上实现 RAII
- 使用 Powershell 命令将 cpp 文件的文件夹编译为 GNU 的 g++
- 使文件夹中的所有文件可供所有项目(子目录)访问
- C++中的文件和文件夹操作
- 如果文件夹不存在,则创建文件夹,如果存在,则不执行任何操作
- SHFile操作无法删除文件夹
- SHFile操作不会移动文件夹的所有内容