CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

Declaring how the file will be used

In different applications, we may need to open the file in different ways.

We pass different strings as the second parameter to fopen() to declare how we'll use the file:

"r" open for reading
"r+" open for reading and writing
"w" create or truncate file, then open for writing
"w+" create or truncate file, then open for reading and writing
"a" create if necessary, then open for appending (at the end of the file)
"a+" create if necessary, then open for reading and appending

All future operations to read and write an open file are checked against the initial file access mode. Future operations will fail if they do not match the initial declaration.

NOTE - File access mode flag "b" can optionally be specified to open a file in binary mode (described later). This flag has effect only on Windows systems, and is ignored on Linux and macOS. This flag has no effect when reading and writing text files.

 


CITS2002 Systems Programming, Lecture 7, p4, 12th August 2024.