ofstream batch;
batch.open("olustur.bat", ios::out);
batch <<"@echo off\n";
batch.close();
system("olustur.bat");
我想在Windows臨時文件夾內創建olustur.bat。我無法實現它。我是C++的新手,所以這可能嗎?如果是這樣,怎麼樣?Ofstream在Windows臨時目錄下創建一個文件
ofstream batch;
batch.open("olustur.bat", ios::out);
batch <<"@echo off\n";
batch.close();
system("olustur.bat");
我想在Windows臨時文件夾內創建olustur.bat。我無法實現它。我是C++的新手,所以這可能嗎?如果是這樣,怎麼樣?Ofstream在Windows臨時目錄下創建一個文件
可以使用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;
}
其實我的問題是讓它到這裏'batch.open(「olustur.bat」,ios :: out);' –
@RıdvanÇetin聽起來像你需要學習如何連接字符串。 – erip
通常在'C:\用戶\ \應用程序數據\本地\ Temp' –
erip
是但這隻會爲我的電腦工作。 –
Temp也存儲在'%TEMP%'中。根據你編譯C++的方式,你可以使用'std :: getenv'或'GetEnvironmentVariable'。 – erip