0

I'm trying to make a function that writes a .ps1 script. I'm learning fstream functions and methods and I ran in to some troubles. 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.

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:

no instance of overloaded function "std::basic_fstream<_Elem, _Traits>::open [with _Elem=char, _Traits=std::char_traits<char>]" matches the argument list

C2661 'std::basic_fstream<char,std::char_traits<char>>‌​::open': no overloaded function takes 0 arguments

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Alex M.
  • 15
  • 1
  • 9

2 Answers2

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.

This is probably a typo for:

if (file.is_open()) {
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Omg.. Dude, I'm so sorry for wasting your time. I can't belive I'm so dumb... Thank you.. And sorry one more time... – Alex M. Mar 11 '17 at 15:39
0

First of all you don't use name parameter. Second you don't define path1 variable. And you do not need to call fstream::open method if you use fstream's initialization constructor.

    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();
         }
    }
JustRufus
  • 492
  • 1
  • 5
  • 10