Standard Library Functions in C

adplus-dvertising
fopen() Function
Previous Home Next

Description

The C library function, The fopen( ) function opens a file whose name is pointed to by fname and returns the stream that is associated with it. The type of operations that will be allowed on the file are defined by the value of mode.

Declaration

Following is the declaration for fopen() function

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

Parameters

Fname - The filename to associate with the new file being opened.

mode - The mode in which the file is to be opened.

mode description
"r"open an existing file for reading only
"w"open a new file for writing only.
"a"open an existing file for appending.
"r+"open an existing file for both reading and writing.
"w+"open a new file for both reading and writing.
"a+"open an existing file for both reading and appending.

Return Value

The fopen() function returns a FILE stream pointer on success while it returns NULL in case of a failure.

Example

#include <stdio.h>
  main ()
  {
      FILE *fp;
      fp = fopen("data.txt", "r");
      if (fp == NULL) {
        printf("File does not exist,please check!\n");
      }
      fclose(fp);
  }
Previous Home Next