C++ ios::app 和 ios::ate 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12929378/
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
Difference between ios::app and ios::ate
提问by Arun
Possible Duplicate:
C++ Filehandling: Difference between ios:app and ios:ate?
What is the difference between these two file opening modes ?
这两种文件打开方式有什么区别?
ios:ate sets the get/put pointer position to the end of the file => reading/writing will begin from end, but how is it different from ios::app, which again opens a file in append mode...but when I have created a ofstream and opened it in ios:app mode, the put stream pointer still points to the beginning, how does the appending work then ?
ios:ate 将获取/放置指针位置设置为文件末尾=> 读取/写入将从末尾开始,但它与 ios::app 有何不同,后者再次以追加模式打开文件...但是当我已经创建了一个 ofstream 并在 ios:app 模式下打开它,put 流指针仍然指向开头,那么附加如何工作?
Also I understand that ifstream, ofstream and fstream are high level classes to manage the underlying stream buffer. So does it mean that even in ios:app mode i can read data from a file ?
我也明白 ifstream、ofstream 和 fstream 是管理底层流缓冲区的高级类。那么这是否意味着即使在 ios:app 模式下我也可以从文件中读取数据?
回答by tozka
app
comes from 'append' - all output will be added (appended) to the end of the file. In other words you cannot write anywhere else in the file but at the end.
app
来自 'append' - 所有输出都将添加(附加)到文件的末尾。换句话说,您不能在文件中的任何其他地方写入,只能在最后写入。
ate
comes from 'at end' - it sets the stream position at the end of the file when you open it, but you are free to move it around (seek) and write wherever it pleases you.
ate
来自 'at end' - 它在您打开文件时将流位置设置在文件的末尾,但您可以随意移动(寻找)并在您喜欢的任何地方写入。
回答by James Kanze
ate
simply positions you at the end of file after opening, and nothing else. It's not much use on an ofstream
, at least without other flags, since the file will have been truncated anyway, so the beginning is the end. (To avoid truncation, and still be able to write anywhere in the file, you need to or in ios::in
as well, even if you're not going to read.)
ate
打开后只需将您定位在文件的末尾,而不是其他任何内容。ofstream
至少在没有其他标志的情况下用处不大,因为无论如何文件都会被截断,所以开始就是结束。(为了避免截断,并且仍然能够在文件中的任何位置写入ios::in
,即使您不打算阅读,您也需要 或 in 。)
app
prevents the truncation of an existing file, and causes every write to go to the end of the file. Atomically, if possible; if other processes are writing to the same file, your write should still go to the end. Note however that this refers to the actual system level write. If, however, you are writing lines which are less than the buffer size, and you terminate each line with std::endl
, you can count on each line being appended atomically, regardless of what other processes might be doing with the file. To be effective, you'll probably want to use pubsetbuf
on the filebuf
as well, to ensure a minimum buffer size.
app
防止截断现有文件,并使每次写入都转到文件末尾。如果可能,原子地;如果其他进程正在写入同一个文件,您的写入仍应进行到最后。但是请注意,这是指实际的系统级写入。但是,如果您正在写入小于缓冲区大小的行,并且用 终止每一行std::endl
,则可以指望以原子方式附加每一行,而不管其他进程可能对文件做什么。为了有效,您可能还想使用pubsetbuf
on filebuf
,以确保最小缓冲区大小。
In practice, I don't think I've ever used either of them, or found them of any use. The buffering issues with app
, in particular, have generally led me to write my own streambuf
, with conceptually unlimited buffering (an std::vector<char>
as buffer), which opens the underlying system file with the equivalent of app
, but guarantees only writing to it when explicitly flushed (as with `std::endl).
在实践中,我认为我从来没有使用过它们中的任何一个,或者发现它们有任何用处。的缓冲问题app
,尤其是,通常导致我编写自己的streambuf
,具有概念上无限的缓冲(std::vector<char>
作为缓冲区),它打开与 等效的底层系统文件app
,但保证仅在显式刷新时写入它(如`std::endl)。
回答by Some programmer dude
If you look at e.g. this reference, you will see:
如果您查看例如此参考资料,您将看到:
app seek to the end of stream before each write
and
和
ate seek to the end of stream immediately after open
This means that ios::app
only writes at the end, but that ios::ate
reads and writes at the end by default. However, with ios::ate
you can seek freely in the file, but with ios::app
you will alwayswrite at the end, no matter what position you set for the writing pointer.
这意味着ios::app
只在最后写入,但ios::ate
默认情况下在最后读取和写入。然而,ios::ate
你可以在文件中自由寻找,但ios::app
你总是在最后写的,不管你在写指针设置在什么位置。