流开放模式:饮食与应用 [英] ofstream open modes: ate vs app

查看:94
本文介绍了流开放模式:饮食与应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题询问appate以及答案和 cppreference 都暗示唯一的区别是app表示在每次写操作之前将写游标放在文件的末尾,而ate意味着只在打开时才将写游标放在文件的末尾.

This question asks the difference between app and ate and both the answers and cppreference imply that the only difference is that app means that the write cursor is put at the end of the file before each write operation, whereas ate means that the write cursor is put at the end of the file only when opening.

我实际看到的(在VS 2012中)是指定ate 会丢弃现有文件的内容,而app不会(将新内容附加到先前存在的文件中)内容).换句话说,ate似乎暗指trunc.

What I'm actually seeing (in VS 2012) is that specifying ate discards the contents of an existing file, whereas app does not (it appends new content to the previously existing content). In other words, ate seems to imply trunc.

以下语句将"Hello"追加到现有文件:

The following statement appends "Hello" to an existing file:

ofstream("trace.log", ios_base::out|ios_base::app) << "Hello\n";

但是以下语句仅用"Hello"替换文件的内容:

But the following statement replaces the contents of the file with just "Hello":

ofstream("trace.log", ios_base::out|ios_base::ate) << "Hello\n";

用于VS 6.0的MSDN文档暗示不应发生这种情况(但在更高版本的Visual Studio中似乎已经撤消了这一句话):

The MSDN documentation for VS 6.0 implies that shouldn't happen (but this sentence seems to have been withdrawn in later versions of Visual Studio):

ios :: trunc :如果文件已经存在,则其内容将被丢弃. 如果指定了ios :: out且ios :: ate,ios :: app, 和ios:in未指定.

ios::trunc: If the file already exists, its contents are discarded. This mode is implied if ios::out is specified, and ios::ate, ios::app, and ios:in are not specified.

推荐答案

您需要将std::ios::instd::ios::ate组合在一起,然后查找文件的结尾并附加文本:

You need to combine std::ios::in with std::ios::ate then seek the end of the file and append the text:

考虑一下,我有一个包含以下内容的文件"data.txt":

Consider I have a file "data.txt" which contains this line:

"Hello there how are you today? Ok fine thanx. you?"

现在我打开它:

1:std :: ios :: app:

1: std::ios::app:

std::ofstream out("data.txt", std::ios::app);

out.seekp(10); // I want to move the write pointer to position 10

out << "This line will be appended to the end of the file";

out.close();

结果不是我想要的:不移动写指针,而仅将文本附加到末尾.

The result is not what I wanted: No moving the write pointer but only the text is always appended to the end.

2:std :: ios :: ate:

2: std::ios::ate:

std::ofstream out2("data.txt", std::ios::ate);

out2 << "This line will be ate to the end of the file";

out2.close();

上面的结果不是我不需要附加任何文本,而是内容被截断了!

The result above is not what I wanted no text appended but the content is truncated!

要解决此问题,请结合使用atein:

To solve it combine ate with in:

std::ofstream out2("data.txt", std::ios::ate | std::ios::in);

out2 << "This line will be ate to the end of the file";

out2.close();

现在将文本添加到末尾,但有什么区别:

Now the text is appended to the end but what is the difference:

正如我所说,应用程序不允许移动写指针,但是吃了可以.

As I said app doesn't allow moving the write pointer but ate does.

std::ofstream out2("data.txt", std::ios::ate | std::ios::in);

out2.seekp(5, std::ios::end); // add the content after the end with 5 positions.

out2 << "This line will be ate to the end of the file";

out2.close();

在我们上面,我们可以将写入指针移动到我们想要的位置,而使用应用程序则无法.

Above we can move the write pointer to the position we want while with app we cannot.

这篇关于流开放模式:饮食与应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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