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
C++ Filehandling: Difference between ios::app and ios::ate?
提问by Adam_G
What's the difference between ios::ate
and ios:app
when writing to a file.
In my view, ios::app
gives you the ability to move around in the file, whereas with ios::ate
it can only read/write at the end of the file. Is this correct?
写入文件时ios::ate
和ios:app
写入文件时有什么区别。
在我看来,ios::app
使您能够在文件中移动,而ios::ate
它只能在文件末尾读/写。这样对吗?
回答by Jon Purdy
It's the other way around. When ios::ate
is set, the initial position will be the end of the file, but you are free to seek thereafter. When ios::app
is 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 中):
app
seek to end before each write
ate
open and seek to end immediately after opening
app
在每次写入之前寻求结束
ate
打开并寻求打开后立即结束
With ios::app
the 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::ate
puts your position to the end of the file when you open it. ios::app
instead 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::ate
will override anything that was added to the file by the other program since your program opened it. ios:app
will 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::ate
option is for input and output operations and
ios::app
allows us to add data to the end of file.
该ios::ate
选项用于输入和输出操作,并
ios::app
允许我们将数据添加到文件末尾。