简体   繁体   English

C ++ Fstream字符串组合错误

[英]c++ Fstream string combination error

I'm trying to make a function that writes a .ps1 script. 我试图做一个写.ps1脚本的函数。 I'm learning fstream functions and methods and I ran in to some troubles. 我正在学习fstream函数和方法,遇到了一些麻烦。 I can't figure a way to make Fstream create the file at the given path (path1) and add in the same time a given name for the file and the extension. 我找不到让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();
    }
}

I get the following error message (on the if line) during compilation: 在编译期间,我收到以下错误消息(在if行上):

no instance of overloaded function " std::basic_fstream<_Elem, _Traits>::open [with _Elem= char , _Traits= std::char_traits<char> ]" matches the argument list 没有重载函数实例“ std::basic_fstream<_Elem, _Traits>::open [with _Elem = char ,_Traits = std::char_traits<char> ]”与参数列表匹配

C2661 ' std::basic_fstream<char,std::char_traits<char>>‌​::open ': no overloaded function takes 0 arguments C2661'std std::basic_fstream<char,std::char_traits<char>>‌​::open ':没有重载函数接受0个参数

if (file.open()) {

Look at a reference : as the error message states, there isn't a member function of fstream called open that takes no arguments. 看一下参考资料 :错误消息指出, fstream的成员函数open没有任何参数。

This is probably a typo for: 这可能是以下方面的错别字:

if (file.is_open()) {

First of all you don't use name parameter. 首先,您不使用name参数。 Second you don't define path1 variable. 其次,您不定义path1变量。 And you do not need to call fstream::open method if you use fstream's initialization constructor . 如果您使用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