C++ c++;Fstream字符串组合错误

C++ c++;Fstream字符串组合错误,c++,C++,我正在尝试创建一个写.ps1脚本的函数。我正在学习fstream函数和方法,遇到了一些麻烦。我想不出一种方法可以让Fstream在给定路径(path1)上创建文件,同时为文件和扩展名添加一个给定的名称 void write(string s, string name) { ostringstream fille; fille << "$client = new-object System.Net.WebClient\n" << s; string

我正在尝试创建一个写.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();
    }
}
void写入(字符串s,字符串名称){
ostringstream fille;
菲勒
请看:正如错误消息所述,
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();
         }
    }
void写入(常量std::string&s,常量std::string&name)
{
std::string fil(“$client=new object System.Net.WebClient\n”);
fil+=s;
std::ostringstream路径;

路径什么是
path1
?路径1是一个带有路径的字符串:“C:/Folder/”您需要提供您想要打开的模式。如果我没有弄错,默认值是只读的。或者您可以使用ofstream,它将默认为writing。我将尝试一下!有什么问题吗?“我不能”这难道不是一个有效的问题吗?你收到错误消息了吗?是不是发生了你预料不到的事情?是不是发生了你预料不到的事情?如果你想让我们帮你解决问题,你必须告诉我们是什么。天哪……老兄,我很抱歉浪费了你的时间。我真不敢相信我这么笨……谢谢你…再一次抱歉…谢谢你让我暴露出来!@JustRufs如果我使用像:%Appdata%/文件夹这样的路径,它会工作吗?对我来说,目前它不工作。请看:所以我必须看看如何在变量中存储cmd命令,对吗?试试这个:
#include std::string Appdata(getenv(“Appdata”))
目前我没有带windows系统的PC/笔记本电脑,因此无法尝试。
    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();
         }
    }