为什么是stream(“log.txt”,ios :: app | ios :: trunc);总是失败? [英] Why does ofstream("log.txt", ios::app|ios::trunc); always fail?

查看:406
本文介绍了为什么是stream(“log.txt”,ios :: app | ios :: trunc);总是失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码是使用VC ++ 2012年11月CTP在Windows 7 x64上编译的。

  #include< fstream& 

using namespace std;

int main()
{
ofstream fout(log.txt,ios :: app | ios :: trunc);
if(!fout)
{
cout< 发生错误! << endl; //总是去这里!为什么?
}
}

cppreference.com网站不说 ios :: app 不能与 ios :: trunc 组合。



exact 语义 ios :: app ios :: trunc

c> c>

c>

在表132中的C ++ 11:

  + ---------------- ------------------- + ------------------- + 
| ios_base标志组合| stdio等效|
|二进制输出trunc应用程序| |
+ ----------------------------------- + --------- ---------- +
| + | w|
| + + a|
| + | a|
| + + w|
| + | r|
| + + | r +|
| + + + w +|
| + + + a +|
| + + a +|
+ ----------------------------------- + --------- ---------- +
| + + wb|
| + + + ab|
| + + ab|
| + + + wb|
| + + rb|
| + + + r + b|
| + + + + w + b|
| + + + + a + b|
| + + + a + b|
+ ----------------------------------- + --------- ---------- +

正如您所看到的,


[C ++ 11:27.9.1.4/ 2]: [..] 如果模式不是表中显示的某些标志组合, 。


这些是语义。



[C ++ 11:27.9.1.7/2] & [C ++ 11:27.9.1.11/2] 显示模式从流对象传递到缓冲对象。 b $ b

The following code was compiled with VC++ Nov 2012 CTP on Windows 7 x64.

#include <fstream>

using namespace std;

int main()
{
    ofstream fout("log.txt", ios::app|ios::trunc);
    if (!fout)
    {
        cout << "An error occurred!" << endl; // Always go here! Why?
    }
}

The cppreference.com website doesn't say that ios::app cannot be combined with ios::trunc.

What are the exact semantics of ios::app and ios::trunc?

解决方案

The filebuf constructor to which these flags are passed has behaviours based on those flags defined in Table 132 in C++11:

+-----------------------------------+-------------------+
|     ios_base flag combination     |  stdio equivalent |
| binary  in    out    trunc    app |                   |
+-----------------------------------+-------------------+
|               +                   |  "w"              |
|               +               +   |  "a"              |
|                               +   |  "a"              |
|               +       +           |  "w"              |
|        +                          |  "r"              |
|        +      +                   |  "r+"             |
|        +      +       +           |  "w+"             |
|        +      +               +   |  "a+"             |
|        +                      +   |  "a+"             |
+-----------------------------------+-------------------+
|   +           +                   |  "wb"             |
|   +           +               +   |  "ab"             |
|   +                           +   |  "ab"             |
|   +           +       +           |  "wb"             |
|   +    +                          |  "rb"             |
|   +    +      +                   |  "r+b"            |
|   +    +      +       +           |  "w+b"            |
|   +    +      +               +   |  "a+b"            |
|   +    +                      +   |  "a+b"            |
+-----------------------------------+-------------------+

As you can see, your flag combination is not found in that table.

[C++11: 27.9.1.4/2]: [..] If mode is not some combination of flags shown in the table then the open fails.

Those are the semantics.

[C++11: 27.9.1.7/2] & [C++11: 27.9.1.11/2] show us that the mode is passed from the stream object to the buffer object.

这篇关于为什么是stream(“log.txt”,ios :: app | ios :: trunc);总是失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆