在使用mkdir()创建一个文件夹后,我如何让程序将文件和其他信息保存到新文件夹中?
After using mkdir() to create a folder how do I have the program save files and other information into the the new folder
所以这个程序在程序本身中创建了一个文件夹,这很好,但是我如何让它将新文件保存在它刚刚创建的文件夹中呢?
#include <iostream>
#include <direct.h>
#include <string>
#include <fstream>
using namespace std;
string newFolder = "Example";
int main()
{
_mkdir((newFolder.c_str()));
fstream inout;
inout.open("hello.txt",ios::out);
inout << " This is a test";
inout.close();
return 0;
}
您需要创建包含目录和文件名的pathname。由于std::string
提供了对operator+
的覆盖,因此这很容易。以下建议可以帮助你上路。
inout.open(newFolder + "/hello.txt");
如果您希望新创建的目录成为当前目录,您还可以尝试添加:
_chdir (newFolder.c_str ())),
调用_mkdir
相关文章:
- 如何将更多文件夹添加到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++
- 使文件夹中的所有文件可供所有项目(子目录)访问
- 无法使用 CMake 从其他文件夹添加源文件
- 如何在安卓工作室中将c ++头文件从一个文件夹导入到另一个文件夹?
- 对单元测试(googletest)和项目/文件夹/文件的混淆
- 设计文件夹/文件系统