【问题标题】:functions to write information to a file in C++在 C++ 中将信息写入文件的函数
【发布时间】:2014-05-13 19:50:52
【问题描述】:

我一直在为学校做一个项目,但遇到了一个问题。我试图避免对程序中的所有内容进行硬编码,而我的部分要求是使用 fstream。这是引发错误的原因。我使用 G++ 作为我的编译器。

void order::printToFile(string file)
{
ofstream output;

try
{
    output.open(file, ios::app);
}

catch(...)
{
    cerr << "An error has occurred";
}

output << this->info.orderID << setw(10) << this->info.type << setw(10) << this->info.quantity << setw(10) << this->info.zip << setw(10) << (this->info.shipCost + this->info.wholesale) << setw(10) << this->info.profit << endl << endl;

output.close();

}

它给了我以下错误:

没有匹配的函数可以调用'std::basic ofstream&lt;char&gt;::open( std::string&amp;, const openmode&amp;)'

有人可以帮我解决这个问题吗?谢谢

【问题讨论】:

    标签: c++ c++11 fstream ofstream


    【解决方案1】:

    没有匹配的函数可以调用'std::basic ofstream&lt;char&gt;::open( std::string&amp;, const openmode&amp;)'

    “无匹配函数”错误意味着编译器搜索但找不到与调用站点提供的参数匹配的重载。 C++11 之前的open() 有一个重载,它采用char const* 类型的缓冲区。这已更新,除了第一个重载之外,open() 现在支持std::string const&amp; 类型的参数。

    问题一定是你的编译器不支持 C++11。将-std=c++11 添加到命令行应该可以解决问题。另一方面,如果您不能这样做,您可以随时使用c_str() 获取指向缓​​冲区的指针:

    output.open(file.c_str(), ios::app);
    //              ^^^^^^^^
    

    您应该知道的另一件事是 IOStreams 默认设计为不抛出异常。相反,它们以称为“流状态”的位掩码类型的形式反映流错误。它可以通过使用布尔运算符方法流支持来访问。

    您可以通过在exceptions() 掩码中设置适当的位来启用异常,但对于这样一个简单的示例,我不建议这样做。只需在打开后检查流就足够了:

    if (std::ofstream output(file.c_str(), std::ios_base::app)) {
        output << "...";
    }
    else {
        std::cerr << "An error has occurred.";
    }
    

    最后,流不需要手动关闭。当定义它们的范围结束时,它们的析构函数将被调用,从而自动释放文件资源。仅在您希望查看它是否可以工作,或者您不再需要该文件并希望立即刷新输出的情况下才需要调用 close()

    【讨论】:

      【解决方案2】:

      也许你的编译器不是 C++11。尝试改变

      output.open(file, ios::app);
      

      output.open(file.c_str(), ios::app);
      

      【讨论】:

        【解决方案3】:

        在 C++11 中添加了采用 std::string 参数的 ofstream 构造函数。假设您的编译器支持它,您需要启用 C++11 模式(-std=c++11 用于 gcc 和 clang)。否则,将函数调用更改为:

        output.open(file.c_str(), ios::app);
        

        另外,请注意,如果ofstream 无法打开文件,它不会导致异常,除非您明确启用异常。

        output.exceptions(std::ofstream::failbit);
        output.open(file.c_str(), ios::app); // will throw exception if open fails
        

        另一种选择是打开文件,然后检查是否成功

        output.open(file.c_str(), ios::app);
        if(!output) {
          // error occurred, handle it
        }
        

        【讨论】:

        • @Richard 请不要在测试您的更改之前建议代码编辑。 ofstream::open 返回void
        【解决方案4】:

        这适用于我在 Ubuntu 14.04 上使用 g++ 版本 4.8.2。

        #include <iostream>
        #include <fstream>
        #include <string>
        
        using namespace std;
        
        void printToFile (string fname) {
            ofstream output;
        
            try {
                // NOTE: passing fname.c_str() not just fname
                output.open (fname.c_str(), ios::app);
            } catch (std::exception e) {
                cout << "err occurred: " << e.what() << endl;
            }
        
            output << "foo bar baz" << endl;
            output.close ();
        }
        
        int main () {
            printToFile ("foo.txt");
            return 0;
        }
        

        我相信您的问题是您尝试使用std::string 而不是char * 作为第一个参数来调用open。请参阅上面代码中的注释。

        【讨论】:

        • 是的,抱歉,将 output 重命名为 out 完全无关紧要。我在写帖子的时候发现了问题,忘记删除了。
        猜你喜欢
        • 1970-01-01
        • 2013-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多