gpt4 book ai didi

c++ - Ofstream 在 Windows 临时目录中创建一个文件

转载 作者:可可西里 更新时间:2023-11-01 11:47:43 24 4
gpt4 key购买 nike

ofstream batch;
batch.open("olustur.bat", ios::out);
batch <<"@echo off\n";
batch.close();
system("olustur.bat");

我想在 Windows 临时文件夹中创建 olustur.bat。我无法实现它。我是 C++ 的新手,这可能吗?如果是,怎么办?

最佳答案

您可以使用 Win32 API GetTempPath()函数检索临时文件夹的完整路径,然后使用 std::ofstream 将文件写入其中。

#include <iostream>
#include <windows.h>
#include <string>
#include <fstream>

using namespace std;

int main()
{
CHAR czTempPath[MAX_PATH] = {0};
GetTempPathA(MAX_PATH, czTempPath); // retrieving temp path
cout << czTempPath << endl;

string sPath = czTempPath;
sPath += "olustur.bat"; // adding my file.bat

ofstream batch;
batch.open(sPath.c_str());
batch << "@echo off\n";
batch.close();

system(sPath.c_str());

return 0;
}

关于c++ - Ofstream 在 Windows 临时目录中创建一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40229477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com