ofstreamvariable.open支持预定的字符串变量吗?

发布于 2025-01-03 20:07:54 字数 534 浏览 0 评论 0原文

我的 IDE 在最​​后一行的“文件名”变量上存在问题。有人可以告诉我为什么吗?

    switch(filename_selection)
    {
        case 1: filename_selection = 1;
        filename = "foo3.sql";
        break;

        case 2: filename_selection = 2;
        filename = "foo2.sql";
        break;

        case 3: filename_selection = 3;
        filename = "foo1.sql";
        break;

        default:
        cout << "Invalid selection." << endl;
        break;
    }
    ofstream File;
    File.open(filename, ios::out | ios::trunc);

My IDE is having issues with the "filename" variable on the last line. Can someone point out to me why?

    switch(filename_selection)
    {
        case 1: filename_selection = 1;
        filename = "foo3.sql";
        break;

        case 2: filename_selection = 2;
        filename = "foo2.sql";
        break;

        case 3: filename_selection = 3;
        filename = "foo1.sql";
        break;

        default:
        cout << "Invalid selection." << endl;
        break;
    }
    ofstream File;
    File.open(filename, ios::out | ios::trunc);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琉璃梦幻 2025-01-10 20:07:54

我的水晶球今天有点浑浊,但我想我能看到一些东西......


您的文件名被声明为std::string filename;。遗憾的是,在 C++03 中,std::(i|o)fstream 类没有接受 std::string 变量的构造函数,只有 char const* 的。

解决方案:传递filename.c_str()

My crystal ball is a bit cloudy today, but I think I can see something...

<psychic-powers>
Your filename is declared as std::string filename;. Sadly, in C++03, std::(i|o)fstream classes didn't have constructors that accept std::string variables, only char const* ones.

Solution: Pass filename.c_str().
</psychic-powers>

孤芳又自赏 2025-01-10 20:07:54

假设filenamestd::string类型,那么你不能将它直接传递给ofstream构造函数:你需要c_str()的力量< /strong>

switch(filename_selection)
{
  case 1:
    //filename_selection = 1; WHAT IS THIS?
    filename = "foo3.sql";
    break;

  case 2:
    ///filename_selection = 2; ???
    filename = "foo2.sql";
    break;

  case 3:
    ///filename_selection = 3; ???
    filename = "foo1.sql";
    break;

  default:
    cout << "Invalid selection." << endl;
    break;
}
ofstream File;
File.open(filename.c_str(), // <<<
          ios::out | ios::trunc);

另外,您似乎误解了如何使用 switch 语句

Assuming that filename is of type std::string, then you can't pass it directly to the ofstream constructor: you need the power of c_str()

switch(filename_selection)
{
  case 1:
    //filename_selection = 1; WHAT IS THIS?
    filename = "foo3.sql";
    break;

  case 2:
    ///filename_selection = 2; ???
    filename = "foo2.sql";
    break;

  case 3:
    ///filename_selection = 3; ???
    filename = "foo1.sql";
    break;

  default:
    cout << "Invalid selection." << endl;
    break;
}
ofstream File;
File.open(filename.c_str(), // <<<
          ios::out | ios::trunc);

Also you seem to have misunderstood how to use the switch statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文