File Handling
File Handling
In C
programming language, the programs store results, and other data of the
program to a file using file handling in C. Also, we can extract/fetch data
from a file to work with it in the program.
The operations that you can perform on a File in C are −
● Creating a new file
● Opening an existing file
● Reading data from an existing file
● Writing data to a file
● Moving data to a specific location on the file
● Closing the file
Creating or opening file using fopen()
The fopen() function is used to create a new file or open an existing file
in C. The fopen function is defined in the stdio.h header file.
Now, lets see the syntax for creation of a new file or opening a file
file = fopen(“file_name”, “mode”)
This is a common syntax for both opening and creating a file in C.
Mode = “r” − open for reading, this mode will open the file for reading purpose only, i.e. the contents
can only be viewed, nothing else like edits can be done to it.
This mode cannot create a new file and open() returns NULL, if we try to create a new file using this
mode.
Mode = “rb” − open for reading in binary mode, this mode will open the file for reading in binary
mode only, i.e. the contents can only be viewed and nothing else like edits can be done to it.
This mode cannot create a new file and open() returns NULL, if we try to create a new file using this
mode.
Mode = “w” − open for writing only, this mode will open the file if present in the current directory for
writing only i.e. reading operation cannot be performed. If the file is not present in the current
directory, the program will create a new file and open it for writing.
If we open a file that contains some text in it, the contents will be overwritten.
Mode = “wb” − open for writing in binary mode, this mode will open the file if present in the current
directory for writing in binary mode i.e. reading operation cannot be performed. If the file is not present
in the current directory, the program will create a new file and open it for writing in binary mode.
If we open a file that contains some text in it, the contents will be overwritten.
Mode = “a” − open for append only, this mode will open the file if present in the current directory
for writing only i.e. reading operation cannot be performed. If the file is not present in the current
directory, the program will create a new file and open it for writing. If we open a file that contains some
text in it, the contents will not be overwritten; instead the new text will be added after the existing text
in the file.
Mode = “ab” − open for append in binary, this mode will open the file if present in the current
directory for writing in binary only i.e. reading operation cannot be performed. If the file is not present
in the current directory, the program will create a new file and open it for writing in binary.
If we open a file that contains some text in it, the contents will not be overwritten; instead the new text
will be added after the existing text in the file.
Mode = “r+” − open for reading and writing both, this mode will open the file for both reading and
writing purposes i.e. both read and write operations can be performed to the file.
This mode cannot create a new file and open() returns NULL, if we try to create a new file using this
mode.
If we open a file that contains some text in it and write something, the contents will be overwritten.
Mode = “rb+” − open for reading in binary mode, this mode will open the file for reading in binary
mode only, i.e. the contents can only be viewed and nothing else like edits can be done to it.
This mode cannot create a new file and open() returns NULL, if we try to create a new file using this
mode.
If we open a file that contains some text in it and write something, the contents will be overwritten.
Mode = “w” − open for writing and reading, this mode will open the file if present in the current
directory for writing and reading operation both. If the file is not present in the current directory, the
program will create a new file and open it for reading and writing.
If we open a file that contains some text in it, the contents will be overwritten.
fscanf()
The fscanf() function is used to read character set i.e strings from the file. It returns the EOF, when all
the content of the file are read by it.
Syntax
int fscanf(FILE *stream, const char *charPointer[])
fgets()
The fget() function in C is used to read string from the stream.
Syntax
char* fgets(char *string, int length, FILE *stream)
fgetc()
The fgetc() function in C is used to return a single character from the file. It gets a character from the
file and returns EOF at the end of file.
Syntax
char* fgetc(FILE *stream)
Syntax
int fprintf(FILE *stream, char *string[])
fputf()
The fputf() function in C can be used to write to a file. It is used to write a line (character line) to the
file.
Syntax
int fputs(const char *string, FILE *stream)
fputc()
The fputc() function is used to write a single character to the file.
Syntax
int fputc(char character , FILE *stream)
fclose()
The fclose() function is used to close the open file. We should close the file after performing
operations on it to save the operations that we have applied to it.
Syntax
fclose(FILE *stream)