簡體   English   中英

Ofstream在Windows臨時目錄中創建文件

[英]Ofstream create a file in Windows temp directory

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

我想在Windows臨時文件夾中創建olustur.bat。 我無法實現它。 我是C ++的新手,這可能嗎? 如果是這樣,怎么辦?

您可以使用Win32 API GetTempPath()函數來檢索temp文件夾的完整路徑,然后使用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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM