17.5 – Need of Data File – fopen()

by subbu on October 8, 2014

So far, we have been accessing the data from the console input device and storing into the variables and presenting the processed data onto the console output device. It is OK for learning and experimenting with C but doesn’t enough to develop common business applications.

It is because memory of variables allocate in the primary memory (RAM). Data stored in the variables would be lost on completion of program execution because variables allocated in the primary memory will be de-allocated on completion of program execution. So the data given to the program for one execution may not be available for the second execution.

The main purpose of any business application is to store the data permanently for future reference. It is only possible by storing the information (processed data) on any secondary storage device as a file.

What is file?

File is a collection of data stored on the secondary storage device with a name called file name. The data stored within the file can be read, modified and deleted through program using different File I/O functions provided by the language.

According to the data stored in the file, files are classified into

  • Text files
  • Binary files

According to the way the data is stored, files are classified into

  • Unformatted Files
  • Formatted Files

According to the data accessing techniques, files are classified into

  • Sequential access files
  • Random access files.

need of files

Data files in C language:

Every language has its resources to work with the files. C language provides number of pre-defined functions to perform file I/O. Most of these functions are defined with in the header file “stdio.h”

The following functions are used to perform file operations in C language

Functions purpose
fopen() To create a new file or to open an existed file
fclose() To close the opened file
fgetc() To read a character from the file
fputc() To write a character on to the file
fprintf() To write formatted output on to the file
fscanf() To read formatted data from the file
feof() To find end of file
fwrite() To write a record onto the file
fread() To read a record from the file
rewind() Sends the record pointer to the beginning of the file
fseek() Sends the file pointer to the specified place from whence

 

We will discourse all these functions one by one with suitable examples. Let use start with fopen()

fopen() function:

It is the function used to create a new file or to open an existed file. It accepts two arguments that are the file name and mode of operation. It returns the file pointer which has low level details of file. We perform different file operation using this file pointer.

fopen() function

FILE *fp=fopen("igate","w"); /* Creating a file in write mode */

In the above statement first argument “igate” is the name of file and the second argument “w” is instructing the runtime to create a new file with the given name.

Here the function fopen() creates a new file with the name “igate” on the hard disk and the low level details of which are returned as a FILE pointer. Mostly the low level details are like sector, track and segment on which file is being created (address of file).

These details stored in the file pointer helps to perform file operations like writing, reading and appending etc.

Note: If the file “igate” is already existed then file will be over writing without any notice but there is a way of checking before overwriting.

file pointer

FILE *fp=fopen("igate","r");

Here, it opens the existed file to perform read operation

FILE *fp=fopen("igate","a");

Here, it opens the file to add/append additional text to the existed file

Following are some of flags send to fopen() to open the file in different modes. Believe me, you no need to worry about all these technical tings at this juncture. We will conquer in easy steps.

Flag Usage
w Opens the text file in write mode
r Opens the text file in read mode
a Opens the text file in append mode
w+ Opens the text file in write /read mode
r+ Opens the text file in read/write mode
a+ Opens the text file in append/ read mode
wb Opens the binary file in write mode
rb Opens the binary file in read mode
ab Opens the binary file in append mode
wb+ Opens the binary file in write/read mode
rb+ Opens the binary file in read/write mode
ab+ Opens the binary file in append/read mode

 

NULL file pointer

fopen() returns NULL if any thing goes wrong in opening the file. It can be used to move cautiously before performing some operations on the file that may change the data in the file.

Say for example, if we want to check whether a file is already existed with the name before creating a new file, then first we try to open the file with the projected name. if fopen() returns NULL then we proceed to create a new file with the name other wise we stop creating the file with the name to get rid from damage.

#include<stdio.h>
int main()
{
 FILE *fp;
 fp=fopen("igate","r");
 if(fp==NULL)
  printf("File is not existed");
 else
  printf("File is existed");
 return 0;
}

Execution
File is not existed

Here fopen() returns NULL as the file pointer because, file with the name “igate” is not at created

Previous post:

Next post: