File opening modes

File opening modes:

We can perform  different operations on a file based on the file opening modes

Note: If a student had written any 3 of the following then the answer is correct.


Mode

Description

r

Open the file for reading only. If it exists, then the file is opened with the current contents; otherwise, an error occurs.

w

Open the file for writing only. A file with a specified name is created if the file does not exist. The contents are deleted if the file already exists.

a

Open the file for appending (or adding data at the end of the file) data to it. The file is opened with the current contents safe. A file with the specified name is created if the file does not exist.

r+

The existing file is opened to the beginning for both reading and writing.

w+

Same as w except both for reading and writing.

a+

Same as a except both for reading and writing.


Note: The main difference is w+ truncate the file to zero length if it exists or creates a new file if it doesn't. While r+ neither deletes the content nor creates a new file if it doesn't exist.

Drafted on 🌏 by,

Jal



peace


Comments

Popular posts from this blog

C program to read and display book information using structure

Count positive, negative and zero values in an array

Maximum value from array