×

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

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.

Local Labels in C

Anyone who has written programs in the C programming language is required to be familiar with the "go to" and "labels" used in C to navigate between functions. "Local labels"...

4 minutes read.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

Static in C

Static is a keyword that is used in the C programming language. It can be used both as variables and as functions. In other words, it can be declared both...

3 minutes read.

Nested Loops in C Programming Examples

A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a “loop inside the...

6 minutes read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.

Storage class in C

Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and...

3 minutes read.

Fibonacci Series in C Using For Loop

In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers. The syntax of for loop used in the...

4 minutes read.

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.

How to Merge Array in C?

Introduction Merging arrays is a common task for many developers but might be difficult for beginners of C programming. Fortunately, our guide will show you how to quickly merge arrays in...

6 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

How to use sine() function in C

What is Function? The function is a set of statements. It takes input and performs some computation to produce output. The process is a set of codes only achieved when it is...

3 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

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.

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied...

3 minutes read.

Infix to Postfix program in C

Infix Expression: In infix expression, an operator is placed between the two operands. Example: x + y, here operator + is placed between operands x and y. Postfix Expression: In...

3 minutes read.

String in C

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare...

2 minutes read.