File Modes in C
What do C files do?
Large amounts of data are stored in files. C offers many file management features, including the ability to create, open, read, write to, and close files. Relevant data is stored in the file, and C file handling is utilized to manipulate the data.
There is a particular file mode in C for each type of file action. We can create, read, write, add, or edit a file using these file modes.
Mode:
A single character designating the file operation you want to do (read, write, or append):
- w- Writes data to a file.
- r- Reads from a file
- a-Appends additional data to it
More intriguing operations that C files are capable of doing are listed in the list below:
- Creating a new file using the fopen command with the "a" or "a+" or "w" or "w+" attribute.
- (fopenopen) Opening an already-existing file.
- reading from a file (using fgets or fscanf).
- Using fprintf or fputs to write to a file.
- Relocating the file pointer connected to a specified file to a particular location. Rewind and (fseek).
- A file is closed (close).
An illustration of reading data from a file and writing to it is as follows:
#include<stdio.h>
#include<conio.h>
main()
{
FILE *fp;
char ch;
fp = fopen("hello.txt", "w");
printf("Enter data");
while( (ch = getchar()) != EOF) {
putc(ch,fp);
}
fclose(fp);
fp = fopen("hello.txt", "r");
while( (ch = getc(fp)! = EOF)
printf("%c",ch);
fclose(fp);
}
Output:
![File Modes in C](https://static.tutorialandexample.com/c/file-modes-in-c1.png)
This program, called greetings, writes greetings to another file after reading them from a file containing a list of names:
#include <stdio.h>
#include <stdlib.h>
intmain()
{
// Create file pointers.
FILE
*names
=fopen("names.txt",
"r");
FILE
*greet
=fopen("greet.txt",
"w");
// Check that everything is OK.
if
(!
names
||!
greet
){
fprintf(stderr,
"File opening failed!\n");
return
EXIT_FAILURE
;
}
// Greetings time!
char
name
[20];
// Basically, keep on reading until there's nothing left.
while
(fscanf(
names
,"%s",
name
)>
0)
{
fprintf(
greet
,"Hello, %s!",
name
);
}
// Print a message to the terminal to alert the user when you've reached the end.
if
(feof(
names
)){
printf("Greetings are done!\n");
}
return
EXIT_SUCCESS
;
}
Output:
![File Modes in C](https://static.tutorialandexample.com/c/file-modes-in-c2.png)
Access modes like "rb", "wb", "ab", "rb+", "r+b", "wb+", "w+b", "ab+," and "a+b" will be used frequently when handling binary files.
File Modes in C:
Let's examine each of these potential ways to access a file on a disc, searches for the file, and, if it's found, open it in read-only mode.
Opening Modes | Description |
r | Inspects a file. The fopen() function loads the file into memory and makes a reference to the file's first character if it is successfully opened. NULL is returned if fopen() is unable to open the file. |
Rb | Open in binary mode for reading. Fopen() will return NULL if the file is nonexistent. |
W | Examines a file. If the file already exists, the contents are replaced. The file is generated if it doesn't already exist. If the file cannot be opened, returns NULL. |
Wb | Binary writing access is available. If the file already exists, its contents are replaced; otherwise, the file is created. |
A | Examines a file. The fopen() function loads the file into memory and makes a reference to the file's last character if it is successfully opened. The file is created if it doesn't already exist. If the file cannot be opened, returns NULL. |
Ab | Open in binary mode for add. The file's end gets updated with data. If a file doesn't already exist, one will be created. |
r+ | Examines a file. It is successfully opened when fopen() loads it into memory and creates a reference to the first character. It returns NULL in the case if the file cannot be opened. |
rb+ | Open in binary mode for both reading and writing. In the event that the file is empty, fopen() returns NULL. |
w+ | Examines a file. If The contents of the file are overwritten if one already existsIf the file doesn't already exist, one will be created. It returns NULL in the case if the file cannot be opened. |
wb+ | Open in binary mode for both reading and writing. If the file already exists, the contents are replaced. The file will be generated if it doesn't already exist. |
a+ | Examines a file. If The fopen() function loads the file into memory and makes a reference to the file's last character if it is successfully opened. The file is created if it doesn't already exist. It returns NULL if the file can’t be opened. |
ab+ | Open in binary mode for both reading and adding. The file will be generated if it doesn’t have the same type if file before. |