C language content

In C, file modes are used to specify the type of operations that can be performed on a file when it is opened. These modes are strings passed as arguments to the ‘fopen‘ function, which is used to open files. The standard file modes in C are defined in the ‘<stdio.h>‘ library. Here are the details of each file mode:

Basic File Modes

  • “r” (read)

    • Opens a file for reading. The file must exist.
    • The file pointer is positioned at the beginning of the file.
    • Example: FILE *file = fopen("example.txt", "r");
  • “w” (write)

    • Opens a file for writing. If the file exists, its contents are truncated to zero length. If the file does not exist, it is created.
    • The file pointer is positioned at the beginning of the file.
    • Example: FILE *file = fopen("example.txt", "w");
  • “a” (append)

    • Opens a file for writing. If the file exists, the file pointer is positioned at the end of the file. If the file does not exist, it is created.
    • Data is always written to the end of the file.
    • Example: FILE *file = fopen("example.txt", "a");
  • “r+” (read/update)

    • Opens a file for both reading and writing. The file must exist.
    • The file pointer is positioned at the beginning of the file.
    • Example: FILE *file = fopen("example.txt", "r+");
  • “w+” (write/update)

    • Opens a file for both reading and writing. If the file exists, its contents are truncated to zero length. If the file does not exist, it is created.
    • The file pointer is positioned at the beginning of the file.
    • Example: FILE *file = fopen("example.txt", "w+");
  • “a+” (append/update)

    • Opens a file for both reading and writing. If the file exists, the file pointer is positioned at the end of the file. If the file does not exist, it is created.
    • Initially, only writing is allowed. After reading, the file pointer can be moved and both reading and writing are allowed.
    • Example: FILE *file = fopen("example.txt", "a+");

Binary File Modes

Binary modes are similar to the text modes described above, but they are used for binary files. They prevent translation of newline characters and treat the file as a sequence of bytes.

  • “rb” (read binary)

    • Opens a binary file for reading. The file must exist.
    • Example: FILE *file = fopen("example.bin", "rb");
  • “wb” (write binary)

    • Opens a binary file for writing. If the file exists, its contents are truncated to zero length. If the file does not exist, it is created.
    • Example: FILE *file = fopen("example.bin", "wb");
  • “ab” (append binary)

    • Opens a binary file for writing. If the file exists, the file pointer is positioned at the end of the file. If the file does not exist, it is created.
    • Example: FILE *file = fopen("example.bin", "ab");
  • “rb+” (read/update binary)

    • Opens a binary file for both reading and writing. The file must exist.
    • Example: FILE *file = fopen("example.bin", "rb+");
  • “wb+” (write/update binary)

    • Opens a binary file for both reading and writing. If the file exists, its contents are truncated to zero length. If the file does not exist, it is created.
    • Example: FILE *file = fopen("example.bin", "wb+");
  • “ab+” (append/update binary)

    • Opens a binary file for both reading and writing. If the file exists, the file pointer is positioned at the end of the file. If the file does not exist, it is created.
    • Example: FILE *file = fopen("example.bin", "ab+");

Summary of File Mode Behaviors

ModeFile Pointer PositionFile ExistsFile Does Not Exist
“r”BeginningReadError
“w”BeginningTruncateCreate
“a”EndAppendCreate
“r+”BeginningRead/WriteError
“w+”BeginningTruncateCreate
“a+”EndRead/AppendCreate
“rb”BeginningReadError
“wb”BeginningTruncateCreate
“ab”EndAppendCreate
“rb+”BeginningRead/WriteError
“wb+”BeginningTruncateCreate
“ab+”EndRead/AppendCreate

Important Notes

  • File Pointer: A file pointer indicates the current position in the file where the next read or write operation will occur.
  • Error Handling: If fopen fails to open a file, it returns NULL. Always check the return value to ensure the file was opened successfully.
  • Closing Files: Use fclose(FILE *stream) to close an opened file and flush any pending output. It is good practice to close files when they are no longer needed to free system resources.

Example of checking the file opening and closing:

				
					FILE *file = fopen("example.txt", "r");
if (file == NULL) {
    perror("Error opening file");
    return -1;
}

// Perform file operations

if (fclose(file) != 0) {
    perror("Error closing file");
    return -1;
}

				
			

Understanding these file modes helps in handling files efficiently in C programming, ensuring data is read, written, or appended correctly as per the requirements.

Scroll to Top