Documente Academic
Documente Profesional
Documente Cultură
•Syntax:
ptr = fopen(“filepath”, “mode”);
Example:
fp = fopen ( "PR1.C", "r" ) ;
fp = fopen(“E:\\cprogram\\newprogram.txt”, “w”);
Opening modes for Text Files
Mode Operation
r Operations possible – reading from the file. Returns NULL if not present.
w Operations possible – writing to the file. Creates new file if it doesn’t exist.
a Operations possible - adding new contents at the end of file.
r+ Operations possible - reading existing contents, writing newcontents, modifying
existing contents of the file.
w+ Operations possible - writing new contents, reading them back and modifying existing
contents of the file.
a+ Operations possible - reading existing contents, appending new contents to end of file.
Cannot modify existing contents.
Opening modes in Binary Files
Mode Operation
rb Open for reading in binary mode. fopen() returns NULL if file doesn’t exist.
wb Open for writing in binary mode. New file is created if it doesn’t exist.
ab Open to append in binary mode. Data is added to the end of file.
rb+ Open for both reading and writing in binary mode. fopen() returns NULL.
wb+ Open for both reading and writing in binary mode
ab+ Open for both reading and appending in binary mode.
Closing a file