C File management

 C File management

File is a place in the disk where a group of related data is stored.

A File can be used to store a large volume of persistent data. Like many other languages 'C' provides following file management functions,





Following are the most important file management functions available in 'C,'

File Handling Functions:-

function    purpose

fopen ()          Creating a file or opening an existing file
fclose ()          Closing a file
fprintf ()          Writing a block of data to a file
fscanf ()          Reading a block data from a file
getc ()          Reads a single character from a file
putc ()          Writes a single character to a file
getw ()          Reads an integer from a file
putw ()          Writing an integer to a file
fseek ()          Sets the position of a file pointer to a specified location
ftell ()          Returns the current position of a file pointer
rewind ()          Sets the file pointer at the beginning of a file

File Operations:
  1. Creation of a file
  2. Opening a file
  3. Reading a file
  4. Writing to a file
  5. Closing a file

Creation of a file

  •  File String of characters that make up a valid filename for OS 
  •  May contain two parts 
  1.  Primary 
  2.  Optional period with extension 

  •  Examples: a.out, prog.c, temp, text.out

  • Whenever you want to work with a file, the first step is to create a file. A file is nothing but space in a memory where data is stored.
  • To create a file in a 'C' program following syntax is used,


                FILE *fp;
                fp = fopen ("file_name", "mode");

  • In the above syntax, the file is a data structure which is defined in the standard library.
  • fopen is a standard function which is used to open a file.

  • If the file is not present on the system, then it is created and then opened.
  • If a file is already present on the system, then it is directly opened using this function.
  • fp is a file pointer which points to the type file.

Opening a file


Whenever you open or create a file, you have to specify what you are going to do with the file. A file in 'C' programming can be created or opened for reading/writing purposes. A mode is used to specify whether you want to open a file for any of the below-given purposes. Following are the different types of modes in 'C' programming which can be used while working with a file.

  • Opening a file - for creation and edit

  • Opening a file is performed using the fopen() function defined in the stdio.h header file.
  • The syntax for opening a file in standard I/O is
  • ptr = fopen("fileopen","mode");
  • For example,
  • fopen("E:\\cprogram\\newprogram.txt","w");
  • fopen("E:\\cprogram\\oldprogram.bin","rb");

  • Let's suppose the file newprogram.txt doesn't exist in the location E:\cprogram. The first function creates a new file named newprogram.txt and opens it for writing as per the mode 'w'.
  • The writing mode allows you to create and edit (overwrite) the contents of the file.
  • Now let's suppose the second binary file oldprogram.bin exists in the location E:\cprogram. The second function opens the existing file for reading in binary mode 'rb'.
  • The reading mode only allows you to read the file, you cannot write into the file.


Opening Mode Description

r                                 Open a file for reading. If a file is in reading mode, then no data is                                          deleted if a file is already present on a system.
w                                 Open a file for writing. If a file is in writing mode, then a new file                                          is created if a file doesn't exist at all. If a file is already present on a                                      system, then all the data inside the file is truncated, and it is                                                    opened writing purposes.
a                                 Open a file in append mode. If a file is in append mode, then the                                              file is Opened. The content within the file doesn't change.
r+                                 Open for reading and writing from beginning
w+                                 Open for reading and writing, overwriting a file
a+                                 Open for reading and writing, appending to file

In the given syntax, the filename and the mode are specified as strings hence they must always be enclosed within double quotes

Reading data from a File


There are three different functions dedicated to reading data from a file

  • fgetc(file_pointer): It returns the next character from the file pointed to by the file pointer. When the end of the file has been reached, the EOF is sent back.
  • fgets(buffer, n, file_pointer): It reads n-1 characters from the file and stores the string in a buffer in which the NULL character '\0' is appended as the last character.
  • fscanf(file_pointer, conversion_specifiers, variable_adresses): It is used to parse and analyze data. It reads characters from the file and assigns the input to a list of variable pointers variable_adresses using conversion specifiers. Keep in mind that as with scanf, fscanf stops reading a string when space or newline is encountered.

Writing a file

The file write operations can be perfomed by the functions fprintf and fputs with similarities to read operations. The snippet for writing to a file is as :

FILE *filePointer ; 
filePointer = fopen(“fileName.txt”, “w”);
fprintf(filePointer, "%s %s %s %d", "We", "are", "in", 2012);

Closing a File

The file (both text and binary) should be closed after reading/writing.
Closing a file is performed using the fclose() function.

fclose(fptr);
Here, fptr is a file pointer associated with the file to be closed.





Post a Comment

0 Comments