×

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

  1. File name.ext
  2. 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.NoMode of operationSyntaxDescription
1.rfopen(“ file name”,” r”);Opens the text file in read mode, and we can read the file
2.wfopen(“ file name”,” w”);Opens the text file in write mode, and we can write the file
3.afopen(“ 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.rbfopen(“ file name”,” rb”);Opens the binary file in read mode, and we can read the file
8.wbfopen(“ file name”,” wb”);Opens the binary file in write mode, and we can write the file
9.abfopen(“ 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:

  1. It is a built-in library function.
  2. fopen() function provides the buffered input which is more faster then the open
  3. fopen() function is portable.
  4. 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.


Related Topics

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

C Program of Fencing the Ground

The college's ground is rectangular. Fencing the ground the management makes the decision to construct a fence around the ground. They planned to wrap a thick rope around the ground...

1 minute read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.

Union in C

Union is a user-defined data type that is used to hold the different types of elements like structure. In union, all members share the same memory location. Syntax: union [union name] { ...

1 minute read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

Keywords in C

A keyword is a word that has a predetermined meaning. The C compiler knows the purposes of the keywords. The objectives of these keywords cannot be modified. Keywords are the...

9 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

Return array from function in C

Return array from function in C C programming does not require the return to a function of a whole array as an argument. However, you can return a pointer to an array without...

2 minutes read.

How to open a C file on android mobile

A C file is a file that has a .c extension and contains code written in the C language. C is a computer programming language developed by Dennis Ritchie at...

4 minutes read.

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

Flow Chart of Do while loop in C

This is a flowchart that represents the process of executing the Do while loop in the C programming language Generally, as we know there are three main components of Do While...

3 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

How to use atoi() function in C

Introduction: The atoi() is a function used in C programming language. This function converts string characters to the integer value. The atoi() function converts the input string to the return type of...

4 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.

GPA Calculator in C

GPA stands for Grade Point Average. This GPA is used to measure the student's academic performance in educational institutes. By using this, segregation takes place and lets us know how...

4 minutes read.

Conio.h in C

Header files are the source files with the extension of .h. In c language header files are referred to as the helping files and they contain definitions of various functions...

4 minutes read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

3 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

Bar3d() function in C Graphics

The bar3d function is used to create a 2-dimensional filled-in rectangular bar. We can also create three-dimensional shapes in C using helpful function. The first step in creating this 3D...

3 minutes read.