What role is played by modes in file operations? Describe the various file mode constants and their meanings.

Mode determines the type of operation (such as reading data from file, writing data from file, closing file )to be done on data file. It tells how the data file will be used after opening it.

File ModeDescriptionPurpose
“r”Read onlyOpen file in read only mode.
“w”Write onlyExisting data of file is truncated and new data is overwritten
“a”AppendExisting data is retained and new data is inserted at the end
“r+”Read and writeBoth reading and writing can be done
“w+”Write and readBoth reading and writng cab done Existing data is truncated and new data is added
“a+”Write and readBoth read and write can be done Existing data is retained and new data is inserted at the end.
error: Content is protected !!