C++ 文件处理:ios::app 和 ios::ate 之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10359702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:56:44  来源:igfitidea点击:

C++ Filehandling: Difference between ios::app and ios::ate?

c++filefstreamc++-standard-library

提问by Adam_G

What's the difference between ios::ateand ios:appwhen writing to a file.
In my view, ios::appgives you the ability to move around in the file, whereas with ios::ateit can only read/write at the end of the file. Is this correct?

写入文件时ios::ateios:app写入文件时有什么区别。
在我看来,ios::app使您能够在文件中移动,而ios::ate它只能在文件末尾读/写。这样对吗?

回答by Jon Purdy

It's the other way around. When ios::ateis set, the initial position will be the end of the file, but you are free to seek thereafter. When ios::appis set, alloutput operations are performed at the end of the file. Since all writes are implicitly preceded by seeks, there is no way to write elsewhere.

正好相反。当ios::ate设定,初始位置将是文件的末尾,但你可以自由此后寻求。当ios::app设置,所有的输出操作都在文件的末尾进行。由于所有写入都隐式地在查找之前,因此无法在其他地方写入。

回答by Steve Jessop

They are specified as follows (in 27.5.3.1.4 of C++11):

它们指定如下(在 C++11 的 27.5.3.1.4 中):

appseek to end before each write

ateopen and seek to end immediately after opening

app在每次写入之前寻求结束

ate打开并寻求打开后立即结束

With ios::appthe write position in the file is "sticky" -- all writes are at the end, no matter where you seek.

随着ios::app文件中写入位置是“粘” -所有的写操作都在最后,无论你在哪里寻找。

回答by Haatschii

It is pretty good documented here.

这里有很好的记录。

ios::ate"sets the stream's position indicator to the end of the stream on opening."

ios::ate“在打开时将流的位置指示器设置为流的末尾。”

ios::app"set the stream's position indicator to the end of the stream before each output operation."

ios::app“在每次输出操作之前,将流的位置指示器设置为流的末尾。”

This means the difference that ios::ateputs your position to the end of the file when you open it. ios::appinstead puts it at the end of the file every time you flush your stream. If for example you two programs that write to the same log file ios::atewill override anything that was added to the file by the other program since your program opened it. ios:appwill instead jump to the end of file each time your program adds a log entry.

这意味着ios::ate当您打开文件时将您的位置置于文件末尾的差异。ios::app而是在每次刷新流时将其放在文件末尾。例如,如果您写入同一个日志文件的两个程序ios::ate将覆盖自您的程序打开另一个程序以来添加到该文件的任何内容。ios:app每次您的程序添加日志条目时,都会跳转到文件末尾。

回答by naveen

ios::app--> "We cannot move the pointer. It will be only at end."

ios::app--> "我们不能移动指针。它只会在最后。"

ios::ate--> "We can move the record pointer to any other place."

ios::ate--> "我们可以将记录指针移动到任何其他地方。"

回答by toijam sonalika devi

The ios::ateoption is for input and output operations and ios::appallows us to add data to the end of file.

ios::ate选项用于输入和输出操作,并 ios::app允许我们将数据添加到文件末尾。