What is the difference between “rb+” and “ab” in fopen()?

て烟熏妆下的殇ゞ 提交于 2019-12-18 09:12:06

问题


I do not understand the difference between the "ab" and "rb+" modes when using fopen() in C.

Why would I choose one instead of the other?


回答1:


With the mode specifiers above the file is open as a text file. In order to open a file as a binary file, a "b" character has to be included in the mode string. This additional "b" character can either be appended at the end of the string (thus making the following compound modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").

From fopen documentation which I advise you read before asking questions. It will give you a lot of information about possible parameters, return values, similar functions etc.

Also, from the same document :

"a" = append: Open file for output at the end of a file. Output operations always write data at the end of the file, expanding it. Repositioning operations (fseek, fsetpos, rewind) are ignored. The file is created if it does not exist.

"r+" = read/update: Open a file for update (both for input and output). The file must exist.



来源:https://stackoverflow.com/questions/43978465/what-is-the-difference-between-rb-and-ab-in-fopen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!