【问题标题】:After using mkdir() to create a folder how do I have the program save files and other information into the the new folder使用 mkdir() 创建文件夹后,如何让程序将文件和其他信息保存到新文件夹中
【发布时间】:2014-03-25 18:51:09
【问题描述】:

所以这个程序在程序本身中创建一个文件夹很好,但是我如何让它将新文件保存在它刚刚创建的文件夹中。

#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;
}

【问题讨论】:

    标签: c++ fstream mkdir


    【解决方案1】:

    您需要创建包含目录和文件名的路径名。由于std::stringoperator+ 提供了覆盖,它就像热苹果派一样简单。以下内容应该可以帮助您顺利上路。

    inout.open(newFolder + "/hello.txt");
    

    【讨论】:

    • 您应该检查对open 的调用的返回值,看看它是否失败。您看到的文本文件可能是旧文件。
    • 好吧,在我更改了一些我在程序中忽略的东西后效果很好,谢谢你的帮助
    【解决方案2】:

    如果希望新建的目录成为当前目录,也可以尝试添加:

    _chdir((newFolder.c_str()));

    调用 _mkdir 之后

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      • 2023-03-29
      • 2023-04-05
      • 1970-01-01
      相关资源
      最近更新 更多