File Opening Modes in C
What are Files?
In computer storage devices, a file is a container for data storage. The complete set of data is lost when a program is stopped. Your data will be kept even if the program is terminated if you store it in a file.
If there are a lot of them, entering all of the data will take a while. You may quickly move your data from one machine to another without making any changes.
Operation of Files:
Four fundamental operations can be carried out on a file:
- Making a new file.
- Opening an existing file.
- Reading information from the file or adding data to it.
- Deleting the document.
Opening a file:
The fopen() function found in the header file stdio.h is used to open files.
Syntax:
pt= fopen("file_name", "mode");
Example:
fopen ("D:javatpointnewprogram.txt", "w") ;
fopen ("D:javatpointoldprogram.bin", "rb") ;
When working with files in C programming, you can use several file opening modes to describe how the file should be retrieved and opened. The second parameter of the fopen() function uses string literals to represent the various file opening modes. When a file is opened, the fopen() method provides a file pointer that can be used to read from or write to the file.
These are C's most popular file-opening methods:
"r" for Read Mode:
- To read the file, open it.
- The function will return NULL if the file doesn't exist.
- The file's beginning contains the file pointer.
"w" for Write Mode.
- Write in the file by opening it.
- The file's contents will be truncated (erased) if it already exists.
- If the file doesn't already exist, one new file will be created.
- The file's beginning contains the file pointer.
"a" for Append Mode:
- Open the file for writing but leave all of its content intact.
- If the file doesn't already exist, one new file will be created.
- Because the file reference is at the end of the file, any writes will add to the material that already exists.
"r+" for Read/Update Mode:
- The file allows both reading and writing.
- The function cannot return NULL without the file being present.
- The file's starting point is where the file pointer is located.
"w+" for Write/Update Mode :
- The file is allowed for reading and writing.
- The file's contents will be shortened (erased) if they already exist.
- The file will be generated if it does not exist already.
- The file's starting point is where the file pointer is located.
"a+" for Update/Append Mode:
- Open the file for reading and writing, but leave the entire contents intact.
- The file will be generated if it does not exist already.
- Since the file's reference is at the end, any writes will be added to the data already present.
"rb" for Read-only Binary Mode:
- Opens a file and allows reading the file in binary mode.
- The function will return NULL if the file doesn't exist.
- The file's beginning contains the file pointer.
"wb" for Binary Write Mode:
- Opens the file and allows writing the file in binary mode.
- The file's contents will be truncated (erased) if it already exists.
- If the file doesn't already exist, one new file will be created.
- The file's beginning contains the file pointer.
"ab" for Binary Append Mode:
- Opens the file and allows writing without trimming its content in binary mode.
- If the file doesn't already exist, one new file will be created.
- Because the file reference is at the end of the file, any writes will add to the material that already exists.
"rb+" for Read/Update Binary Mode:
- Opens the file in binary mode and allows reading and writing.
- The function will return NULL if the file doesn't exist.
- The file's beginning contains the file pointer.
"wb+" for Binary Write/Update Mode:
- Opens the file in binary mode and allows reading and writing.
- The contents of the file will be truncated (erased) if it already exists.
- If the file doesn't already exist, one new file will be created.
- The file's beginning contains the file pointer.
"ab+" for Binary Append/Update Mode:
- Open the file in binary mode for reading and writing without trimming the content.
- If the file doesn't already exist, one new file will be created.
- Because the file reference is at the end of the file, any writes will add to the material that already exists.
Program:
// C program to implement
// the above approach
#include <stdio.h>
#include <string.h>
// Driver code
int main()
{
// Declare the file pointer
FILE* filePointer;
// Get the data to be written in a file
char dataToBeWritten[50]
= "A ComputerScience Portal ";
// Open the existing file Test. c using fopen()
// in write mode using the "w" attribute
filePointer = fopen("Test.txt", "w");
// Check if this filePointer is null
// which maybe if the file does not exist
if (filePointer == NULL) {
printf("Test.txt file failed to open.");
}
else {
printf("The file is now opened.\n");
// Write the dataToBeWritten into the file
if (strlen(dataToBeWritten) > 0) {
// writing in the file using fputs()
fprintf(filePointer, dataToBeWritten);
fprintf(filePointer, "\n");
}
// Closing the file using fclose()
fclose(filePointer);
printf("Data successfully written in file Test.txt\n");
printf("The file is now closed.");
}
return 0;
}
Output:
![File Opening Modes in C](https://static.tutorialandexample.com/c/file-opening-modes-in-c1.png)
Mode | Meaning | Operation |
r | accessible for reading. | fopen() returns NULL if the file is not present. |
rb | Open for reading in binary mode. | fopen() returns NULL if the file is not present. |
w | Writing is acceptable. | The file's contents are overwritten if the file exists already. The new file will be generated if it doesn't already exist. |
wb | Binary writing access is available. | The file's contents are overwritten if the file exists already. The new file will be generated if it doesn't already exist. |
a | Allows adding to file. | This file's end gets updated with data. The new file will be generated if it doesn't already exist. |
ab | Run in binary mode for add. | The file's end gets updated with data. The file will be generated if it doesn't already exist. |
r+ | Both reading and writing are allowed. | fopen() returns NULL if the file is not present. |
rb+ | Open for reading and writing in binary mode. | fopen() returns NULL if the file is not present. |
w+ | Both reading and writing are allowed. | The file's contents are overwritten if the file exists already. The new file will be generated if it doesn't already exist |
wb+ | Open for reading and writing in binary mode. | The file's contents are overwritten if the file exists already. The new file will be generated if it doesn't already exist. |
a+ | Open for reading as well as appending | The new file will be generated if it does not exist already. |
ab+ | Open for reading and adding in binary mode. | The new file will be generated if it does not exist already. |