In this topic today I am going to teach about File Handling In C programming.
What is a file?
A file is an object which holds data, information or commands. Like many other programming languages, C also provides operations in files such as creating, reading, writing, opening and closing.
Let’s dive deep and see how a file is created:
Syntax:
FILE *desired_name;
*desired_name=fopen(“filename” , ”mode”);
Hence the above syntax is used to create a file. There are several file management functions in C, which are:
Function | Purpose |
fopen() | To create or open a file |
fclose() | To close a file |
fprintf() | Writing a block of data to a file |
fscanf() | Reading a block of data from a file |
getc() | Reading a single character from a file |
putc() | Writing a single character in a file |
getw() | Reading an integer from a file |
putw() | Writing an integer to a file |
fseek() | Sets the position of the file pointer to a specific location |
ftell() | Returns the current position of the file pointer |
rewind() | Sets the file pointer at the beginning of the file |
Join Our WhatsApp Group
Join our WhatsApp Group To know more About Programming Language tips and how to start learning any programming language.
Like the file management functions there are several modes while dealing with files in C, they are:
File mode | Description |
r | Open a file for reading. If a file is in reading mode, then no data is deleted if a file is already present on a system |
w | Open a file for writing. If a file is in writing mode, then a new file is created if a file doesn’t exist at all. If a file is already present on a system, then all the data inside the file is truncated, and it is opened for writing purposes |
a | Open a file in append mode. If a file is in append mode, then the file is opened. The content within the file doesn’t change |
r+ | Open for reading and writing from the beginning |
w+ | Open for reading and writing, overwriting a file |
a+ | Open for reading and writing, appending to file |
These are the important modes and file management functions in C which helps in the file operations. This was an introductory part to file management in C and will deal with them practically in the next article.