【问题标题】:Ofstream writes empty file on linuxOfstream在linux上写入空文件
【发布时间】:2010-05-24 09:33:09
【问题描述】:

我有一个使用 ofstream 写入其输出的程序。使用 Visual Studio 编译时,在 Windows 上一切正常,但使用 GCC 编译时,它只会在 Linux 上写入空文件。

ofstream out(path_out_cstr, ofstream::out);
if(out.bad()){
 cout << "Could not write the file" << flush;
}
else{
 cout << "writing";

 out << "Content" << endl;

 if(out.fail()) cout << "writing failed";

 out.flush();
 out.close(); 
}

正在写入的目录有0777权限。

奇怪的是:什么都没写,却没有报错。

gcc --version 是:(Gentoo 4.3.4 p1.0, pie-10.1.5) 4.3.4

我知道代码应该可以工作,所以我更喜欢寻找建议,可能有什么问题,而不是直接修复代码。

编辑:fwrite 似乎以完全相同的方式失败(没有写入任何内容,没有报告错误)。

编辑:我正在我的大学目录上通过 SSH 执行 GCC 和程序,如果它有任何意义的话。我有足够的权限来执行和写入文件(ls .> out.txt 工作得很好),只有我的程序有问题。

感谢您的帮助

【问题讨论】:

  • 在 FC12 下使用 g++ 4.4.3 为我工作
  • 你的程序在Linux下的输出(如cout)是什么?
  • 您是否检查了目录中的可用空间(df -h dirname)?我还会在 strace 下运行程序,看看系统调用失败的原因。
  • 也许该文件已经存在,为空,并且没有写入权限?每次运行前你都会删除它吗?
  • 文件有无无所谓,最终都是空的。

标签: c++ linux gcc ofstream


【解决方案1】:

为我工作,ubuntu g++-4.1。
您是否尝试过执行strace ./test 并查看文件是否有write() 调用?

【讨论】:

  • 是的,写调用在那里
  • 有写调用吗?然后与代码无关。 :S 我猜cat "something" &gt; test_file 有效吗?
  • 哦,现在我看到 strace 输出中隐藏了“超出磁盘配额”。感谢帮助,我不知道 strace。
【解决方案2】:

最可能的解决方案是由于名称或路径的问题,文件无法在构造函数中打开。如果无法打开文件,则将设置 failbit 而不是 badbit 位,因此请对此进行测试而不是使用 bad()

ofstream out(path_out_cstr, ofstream::out); 
if(out.fail()){ 
    cout << "Could not write the file" << flush; 
...

fail() 检查是否设置了故障位或坏位,而坏只检查坏位。顺便说一句,我尝试了你的示例,它没有问题,所以我故意让路径变坏 - 仍然没有问题,但是当我更改为 fail() 时,它在错误的路径上拾起。

【讨论】:

    【解决方案3】:

    如果正在创建文件,我可以知道为什么它不会被写入。

    检查path_out_cstr 的值。在类 Unix 系统上,路径用正斜杠“/”而不是 MS-DOS 风格的反斜杠“\”分隔,这可以解释两个操作系统之间的行为差​​异。


    更新

    由于我们未能catch failbit | badbit问题一时,大家不妨来try的异常处理办法...这个示例会在报第一次失败后停止...

    #include <fstream>
    #include <iostream>
    
    int main(int argc, char* argv[])
    {
        const char* const path_out = argv[1];
    
        std::cerr.exceptions(std::cerr.goodbit);
    
        std::ofstream the_file_stream;
        the_file_stream.exceptions(the_file_stream.badbit | the_file_stream.failbit);
    
        try
        {
            std::cerr << "Opening [" << path_out << "]" << std::endl;
            the_file_stream.open(path_out);
    
            std::cerr << "Writing" << std::endl;
            the_file_stream << "Content" << std::endl;
    
            std::cerr << "Flushing" << std::endl;
            the_file_stream.flush();
    
            std::cerr << "Closing" << std::endl;
            the_file_stream.close();
        }
        catch (const std::ofstream::failure& e)
        {
            std::cerr << "Failure: " << e.what() << std::endl;
        }
    
        return 0;
    }
    

    【讨论】:

    • path_out_cstr 是程序的命令行参数,所以它是我输入的。另外,我猜如果路径错误,文件甚至不会被创建。
    • 你是对的,当然。我提供了一些示例代码,它们可能有助于将来发现类似的错误。
    【解决方案4】:

    使用 g++ 4.4.1 为我工作

    【讨论】:

    • gcc --version 是:(Gentoo 4.3.4 p1.0, pie-10.1.5) 4.3.4 我知道代码应该可以工作,所以我更喜欢寻找建议,可能有什么问题,而不是直接代码修复。
    • @commanderz:我怀疑这是代码问题。您是否尝试过更改输出目录?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    相关资源
    最近更新 更多