week-6
week-6
COMPUTING II
• Two arguments
– a filename (which can include path information leading to the
file’s location)
– a file open mode
• File open mode "w" indicates fopen should open the file for writing
– If the file does not exist, fopen creates the file
– If you open an existing file, fopen discards the file’s contents
without warning
• If the file pointer fopen returns is NULL, the file could not be opened
Mode Description
r Open an existing file for reading.
w Create a file for writing. If the file already exists, discard the current
contents.
a Open or create a file for writing at the end of a file—this is for write
operations that append data to a file.
r+ Open an existing file for update (reading and writing).
w+ Create a file for reading and writing. If the file already exists, discard the
current contents.
a+ Open or create a file for reading and updating where all writing is done
at the end of the file—that is, write operations append data to the file.
Mode Description
rb Open an existing binary file for reading.
wb Create a binary file for writing. If the file already exists, discard the
current contents.
ab Open or create a binary file for writing at the end of the file
(appending).
rb+ Open an existing binary file for update (reading and writing).
wb+ Create a binary file for update. If the file already exists, discard the
current contents.
ab+ Open or create a binary file for update. Writing is done at the end of
the file.
Copyright © 2022 Pearson Education, Inc. All Rights Reserved
11.3 Creating a Sequential-Access
File (7 of 8)
• The new record has more characters than the original record
• The characters beyond the second "o" in "Worthington" will overwrite the
beginning of the next sequential record in the file
• In the formatted input/output model using fprintf and fscanf, fields
and records can vary in size
– 7, 14, – 117, 2074 and 27383 are all ints stored internally in
the same number of bytes, but they’re different sizes when displayed
or written to a file as text