If file doesn't exist: With "r" mode: fopen() fails, returns NULL With "w" mode: new file is created With "a" mode: new file is created
fopen will create a new file in ALL modes EXCEPT those starting with "r"
2nd Parameter Type : const char *
Pointer to read-only string
2nd Parameter
Basic modes: "r" - read only (file must exist) "w" - write only (creates new/truncates existing) "a" - append (creates new/preserves existing) "r+" - read and write (file must exist) "w+" - read and write (create new/truncates existing) "a+" - read and append (creates new/preserves existing) Additional flags: "b" - binary mode (e.g., "rb", "wb", "ab") "t" - text mode (usually default, rarely used explicitly) Important notes: Mode string is always constant ("r" not 'r') Order matters for additional flags (use "rb" not "br") Binary mode is crucial for Windows systems, optional but harmless on Unix
Read more about parameters of fopen in parameters section
The fopenfunction in C language opens the file specified by user defined filename, associates a stream with it using the given mode and returns a pointer to the stream.
fopen is a fundamental C library function that serves as the entry point for all file operations, required before any file reading or writing can occur. It works in tandem with fclose for proper file handling, and it returns FILE pointer that is used by the entire family of file operations like fread, fwrite, fprintf, fscanf, fgets, and fputs. What makes fopen special is its mode parameter that determines the type of operations allowed on the file (read, write, append) and its behavior with existing/non-existing files, as well as its automatic handling of different operating system file access methods through a unified stream interface.
To Summarize the Workflow:
1.
Function attempts to open a file with specified name and mode
2.
Takes two parameters: filename (path to file) and mode (type of access)
3.
Returns FILE pointer on success, NULL on failure
4.
Sets errno when operation fails
5.
Creates new file for write modes if file doesn't exist
6.
Supports various modes: r, w, a, r+, w+, a+ for read/write operations
Read more about return type and value offopen function in return section.
The fopenfunction takes 2
parameters:
•
const char * `filename`: If file doesn't exist: With "r" mode: fopen() fails, returns NULL With "w" mode: new file is created With "a" mode: new file is created
fopen will create a new file in ALL modes EXCEPT those starting with "r"
•
const char * `mode`: Basic modes: "r" - read only (file must exist) "w" - write only (creates new/truncates existing) "a" - append (creates new/preserves existing) "r+" - read and write (file must exist) "w+" - read and write (create new/truncates existing) "a+" - read and append (creates new/preserves existing) Additional flags: "b" - binary mode (e.g., "rb", "wb", "ab") "t" - text mode (usually default, rarely used explicitly) Important notes: Mode string is always constant ("r" not 'r') Order matters for additional flags (use "rb" not "br") Binary mode is crucial for Windows systems, optional but harmless on Unix
The fopen function return value :
Success: Returns pointer to FILE object
Failure: Returns NULL and sets errno to indicate error: EACCES: Permission denied ENOENT: File doesn't exist (for "r" modes) EINVAL: Invalid mode argument EMFILE: Too many open files (and other system-specific errno values)
The FILE pointer returned on success is used for all subsequent operations on the file stream
Output
This example demonstrates how to use `fopen` to open a file for reading. It opens 'example.txt' in read mode, reads its contents line by line, and prints them to the console.