簡體   English   中英

C ++ Fstream字符串組合錯誤

[英]c++ Fstream string combination error

我試圖做一個寫.ps1腳本的函數。 我正在學習fstream函數和方法,遇到了一些麻煩。 我找不到讓Fstream在給定路徑(path1)上創建文件並同時為文件和擴展名添加給定名稱的方法。

void write(string s, string name) {

    ostringstream fille;
    fille << "$client = new-object System.Net.WebClient\n" << s;
    string fil = fille.str();
    ostringstream pat;
    pat << path1 << "/" << ".ps1";
    string path = pat.str();
    fstream file(path);
    if (file.open()) {
        file << fil;
        file.close();
    }
}

在編譯期間,我收到以下錯誤消息(在if行上):

沒有重載函數實例“ std::basic_fstream<_Elem, _Traits>::open [with _Elem = char ,_Traits = std::char_traits<char> ]”與參數列表匹配

C2661'std std::basic_fstream<char,std::char_traits<char>>‌​::open ':沒有重載函數接受0個參數

if (file.open()) {

看一下參考資料 :錯誤消息指出, fstream的成員函數open沒有任何參數。

這可能是以下方面的錯別字:

if (file.is_open()) {

首先,您不使用name參數。 其次,您不定義path1變量。 如果您使用fstream的初始化構造函數,則無需調用fstream::open方法。

    void write( const std::string & s, const std::string & name )
    {
         std::string fil("$client = new-object System.Net.WebClient\n");
         fil += s;

         std::ostringstream path;
         path << "C:/Folder/" << name << ".ps1";

         std::ofstream file( path.str() );
         if ( file.is_open() ) {
             file << fil;
             file.close();
         }
    }

暫無
暫無

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

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