Thursday, December 17, 2020

, , ,

fopen function in c programming with it's example

 fopen c function is used to open a file which is denoted by the file name in c programming.

Syntax for fopen function in c : -

FILE *fopen(const char *filename, const char *mode)

Parameters For for fopen c : -


filename : - this element contain the file name which is going to be open.

Mode :- This string in c contain the method to access the file.

Check the mode for open a file in c programming with the help of fopen c function. 

  • "r" Mode : - This mode opens a file for reading.
  • "w" Mode : - this one use to create a empty file for writing.
  • "a" Mode : - It use to append the data  at the end of the file.
  • "r+" Mode : - This mode open a file to make an update in reading and writing.
  •  "w+" Mode :  - Create an empty file for both reading and writing.
  •  "a+" Mode : - Use to open an file for appending and also for reading.

Return Value for fopen c function : -

  •  fopen function in c return a file pointer otherwise give a null.
  • errno global variable is used to indicate the error in fopen function.

Example program for fopen function in c :

#include <stdio.h>
#include <stdlib.h>

int main () {
   FILE * fp;

   fp = fopen ("file.txt", "w+");
   fprintf(fp, "%s %s %s %d", "We", "are", "in", 2012);
   
   fclose(fp);
   
   return(0);
}


Example for fopen c
Example program for fopen c

0 comments:

Post a Comment