In this post you will learn file handling in c programming language with the help of program for file handling in c.
- What is file handling in c?
- File types in c
- Various operations in file handling in c
- Various functions for file handling in c
- Program for file handling in c
- Working with files in c programming
- Creating a new file/Open a new file
- Closing a file
- C program to read content of file
- People also ask
- People also read
What is file handling in c?
File handling provides a way to perform input and output operations on a file. We can store and retrieve that is also called as output and input of a program respectively.
We can simply input or output data using standard input devices, consider a scenario where you have to perform input and out operation on large dataset. It is so time consuming process to inputting the data and analysing the output data. Hence we can simply use files to perform input and output operations. Another advantage about using files is that we are storing files on the storage devices hence our data is volatile and it is safe until that hardware device crashes.
Before going to inbuilt functions for file handling in c we first have a look at file types in c.
At first we look into different types of files.
File types in c
Source files
Source files are nothing but the files which contains the program code. Source files have the extension .c
.
Header files
These header files contain the prototype of function, and various pre-processors. Header files have the extenion .h
.
Object files
These object files are produced by compiler as output of compiling the source file. These object files end with .o .obj
extensions.
Binary executables
These are the output files of linker after linking library files with various object files. Binary executable files have no extension in unix based systems. In windows based operating systems they end with .exe
extension.
Libraries
It conatins generalised form of a function definition. It is used by more than one source files. It ends with .h
extension. There are two types of library files: 1) Static library 2)Dynamic library.
Various operations in file handling in c
We can perform various file operations such as
- Creating a new file.
- Opening a file.
- Reading a file.
- Writing a file.
- Deleting a file.
- Closing a file.
While carrying out file handling in c programming language we can make use of various inbuilt functions to perform various actions.
Various functions for file handling in c
C library provides various functions to perform file operations. Look at the table for such functions.
Go down to this article and also see example program for file handling in c after reading various functions for file handling in c.
Name | Description |
fopen() | This function opens new or existing file. |
fclose() | This function closes opened file. |
fprintf() | This function writes string to file. |
fscanf() | This function reads string from file. |
fgetc() | This function reads character from file. |
fputc() | This function writes character to file. |
fseek() | This function sets file pointer to given position. |
getw() | This function reads an integer value from file. |
putw() | This function writes an integer value to file. |
ftell() | This function returns current position. |
feof() | This returns whether the file pointer reached end or not. |
rewind() | This function sets file pointer to beginning of the file. |
Program for file handling in c
In this program for file handling in c programming language example we use fopen
to open a file then fclose
to close a file and fprintf
to write data to a file.
//program for file handling in c
#include <stdio.h>
#include<stdlib.h>
int main(int argc, char const *argv[])
{
FILE *file_pointer=fopen("example.txt","a+");
if( fprintf(file_pointer, "Scholarsoul.com\nBe a scholar")>0)
{
printf("Data written to file.");
}
else
{
printf("Error while writing data to file.");
}
fcolse(fp);
return 0;
}
Output
In the console.
Data written to file.
In the file example.txt
Scholarsoul.com
Be a scholar
Working with files in c programming
Inorder to perform various file operations you have to declare a pointer to that file. This pointer points to the file that you want to perform various operations.
You can create pointer to file as shown below:
FILE *file_pointer;
Creating a new file/Open a new file
We can open a file using fopen
function which is defined in stdio.h
header file.
Prototype of fopen
FILE * fopen(const char *file_name, const char *mode);
Syntax to open a file is as shown below:
FILE *file_pointer=fopen("file_name","mode");
Example to open a file is as shown below:
FILE *file_pointer1=fopen("example1.txt","r");
FILE *file_pointer2=fopen("example2.txt","w");
Here we are opening the file ‘example1.txt’ with read mode. Read mode allows us to read contents of that file.
For file_pointer2 we are opening example2.txt with write mode. Write mode allows us to write the contents of the file. Writing to a file may involve clearing contents of the file then writing, writing at the end of the file, wrinting in middle of that file.
Now lets check various modes to open a file.
Mode | Description |
r | Open file for reading. |
w | Open file for writing. |
rb | Open file for reading in binary. |
wb | Open file for writing in binary. |
a | Open file for appending data. |
ab | Open file for appending data in binary. |
r+ | Open file for reading and writing. |
w+ | Open file for reading and writing. |
rb+ | Open file for reading and writing in binary mode. |
wb+ | Open file for reading and writing in binary mode. |
a+ | Open file for reading and appending. |
ab+ | Open file for reading and appending in binary mode. |
Closing a file
After performing input and output operations on a file, it has to be closed. Inorder to close a file fclose
function is used. It is found in stdio.h
header file.
prototype of fclose
int fclose(FILE *file_pointer);
In above example program to read a file we have used fclose(file_pointer);
.
C program to read content of file
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("\n\nScholarsoul.com\nFile is being read.\nContents of the file is:\n\n");
FILE *file_pointer=fopen("C program to read content of file.c","r");
char c;
while(1)
{
c=fgetc(file_pointer);
if(c==EOF)
break;
putchar(c);
}
printf("\n\nFile reading completed.\n\n");
fclose(file_pointer);
return 0;
}
Output
On console:
Scholarsoul.com
File is being read.
Contents of the file is:
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("\n\nScholarsoul.com\nFile is being read.\nContents of the file is:\n\n");
FILE *file_pointer=fopen("C program to read content of file.c","r");
char c;
while(1)
{
c=fgetc(file_pointer);
if(c==EOF)
break;
putchar(c);
}
printf("\n\nFile reading completed.\n\n");
fclose(file_pointer);
return 0;
}
File reading completed.
Code explaination:
We are opening the file named “C program to read content of file.c” using fopen function with read mode.
Then we are making an infinite loop such that it is not infinite it ends when the reading encounters EOF
End of File.
We are reading each character checking the read character such that if it has the value EOF
then we are stopping the loop using break statement to stop reading.
If read character is not EOF
then we are printing it using putchar in c.
People also ask
What is file handling in C?
File handling provides a way to perform input and output operations on a file
What is file and its types?
File is the collection of data stored on various storage devices on computer, mobile devices.
What are the advantages of files in C?
C programming language provides you various efficient way to access files using inbuilt functions.
How do you declare a file?
We can make use of FILE pointer to access a file. We can declare a file like FILE *file_pointer;
People also read
- ftell in c
- feof in c
- fopen in c
- fclose in c
- getw in c
- putw in c
- fseek in c
- fscanf in c
- fprintf in c
- File input output in c