domlao domlao - 1 month ago 7
C Question

What is the difference between a and a+ option in fopen function?

I can't understand the description of the "a" and "a+" option in the C fopen api documentation. The option in "a+" is append and update. What is the meaning of the word update here?

Answer

Here is what the man pages (man fopen) say:

a

Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.

a+

Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.


Which means:

for a+: pointer initially is at the start of the file ( for reading ) but when a write operation is attempted it is moved to the end of the file.

Comments