fopen() function in C
fopen() function, is one of the file handling functions. It is used to open the existing file and perform operations on the file. If the file does not exist, it creates a new file. Operations on files are read and write. fopen() function is present in stdio.h header file.
By using fopen() function, we can open a file in different modes like reading mode, write mode, read and write mode, append mode etc.
Syntax for: fopen(“ file name.ext”, “mode of operation”);
Parameters: fopen() function accepts two parameters
- File name.ext
- Mode of operation ex: r, w, a etc.
Example program on fopen() function:
// C program on fopen() funvtion
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE* mad;
// Creates a file "Hello"
// creating the file n W+ mode
mad = fopen(" Hello.txt", "w+");
// Adding content to the Hello text file
fprintf(mad, "%s %s %s", "Welcome",
"to", "JavaTpoint");
// closes the file pointed by mad
fclose(mad);
return 0;
}
In the above example, we are creating a new text file "Hello" in w+ mode so we can perfume write and read operations on the file. After performing operation we have to close the file.
Output:
Welcome to JavaTpoint
Mode of operation in files:
S.No | Mode of operation | Syntax | Description |
1. | r | fopen(“ file name”,” r”); | Opens the text file in read mode, and we can read the file |
2. | w | fopen(“ file name”,” w”); | Opens the text file in write mode, and we can write the file |
3. | a | fopen(“ file name”,” a”); | Opens the text file in append mode, and we can add content to the file |
4. | r+ | fopen(“ file name”,” r+”); | Open the text file in r+ mode. In r+ mode, we can read and write the file |
5. | w+ | fopen(“ file name”,” w+”); | Open the text file in w+ mode. In w+ mode, we can read and write the file |
6. | a+ | fopen(“ file name”,” a+”); | Open the text file in a+ mode. In a+ mode, we can read and append the file |
7. | rb | fopen(“ file name”,” rb”); | Opens the binary file in read mode, and we can read the file |
8. | wb | fopen(“ file name”,” wb”); | Opens the binary file in write mode, and we can write the file |
9. | ab | fopen(“ file name”,” ab”); | Opens the binary file in append mode, and we can append the file |
10. | rb+ | fopen(“ file name”,” rb+”); | Open the binary file in rb+ mode. In rb+ mode, we can read and write the file |
11. | wb+ | fopen(“ file name”,” wb+”); | Open the binary file in wb+ mode. In wb+ mode, we can read and write the file |
12. | ab+ | fopen(“ file name”,” ab+”); | Open the binary file in ab+ mode. In ab+ mode, we can read and append the file |
The Text file contains integer values, floating point values, characters, special characters etc. Whereas a binary file only contains 0’s and 1’s.
Example to open the file in read mode:
// program to open a file in read mode
#include<stdio.h>
void main( )
{
FILE *mad; // file pointer
char demo;
mad = fopen(" Hello.c","r") ; // opening the file in read mode
while ( 1 )
{
demo = fgetc ( mad );
if ( demo == EOF )
break ;
printf("%c",demo);
}
fclose (mad ); // closing the file
}
// program to open a file in read mode
#include<stdio.h>
void main( )
{
FILE *mad; // file pointer
char demo;
mad = fopen(" Hello.c","r") ; // opening the file in read mode
while ( 1 )
{
demo = fgetc ( mad );
if ( demo == EOF )
break ;
printf("%c",demo);
}
fclose (mad ); // closing the file
}
Advantages of fopen() function:
- It is a built-in library function.
- fopen() function provides the buffered input which is more faster then the open
- fopen() function is portable.
- fopen() function does line ending translation.
Conclusion:
In this article we learned about the fopen()function in C, the advantages of fopen() function in C, different mode of operations on files using fopen() function, parameter passing in fopen() function, Examples on fopen() function, description on mode of operation on files.