C File Handling

A file is a collection of streamed bytes stored on a secondary storage device. These streamed byte can be interpreted as characters, words, lines, paragraphs and pages of a textual document; fields and records belonging to a database; or pixels from a graphical image.Through the file handling function we can create, modify,move or delete files on system.In every programming language it is important to implement file handling function. In C Programming Language a Special set of functions have been designed for handling file operations.

Some of the basic file handling operations are-

  • Open & Close files
  • Read from & Write to files
  • Delete files.

Function for all of the above operations are available in the stdio.h header file.

Opening & Closing Files (fopen() and fclose())

In C Programming Language before we perform any operations on a file, we must open it first, this can be done using the fopen function, which returns the pointer to the required. If the file cannot be opened for any reason then the value NULL will be returned.

Syntax:

The fopen() function is used to open a file and associates an I/O stream with it. This function takes two arguments. The first argument is a pointer to a string containing name of the file to be opened while the second argument is the mode in which the file is to be opened. The mode can be :

r Open for reading
r+ Open for reading and writing
w Open for writing and create the file if it does not exist. If the file exists then make it blank.
w+ Open for reading and writing and create the file if it does not exist. If the file exists then make it blank.
a Open for appending(writing at the end of file) and create the file if it does not exist.
a+ Open for reading and appending and create the file if it does not exist.

Below is the code snippet for opening and closing a file –

The fclose() function is used for closing opened files.

Syntax:

The only argument it accepts is the file pointer.If a program terminates, it automatically closes all opened files. Upon successful completion this function returns 0 else end of file (eof) is returned. In case of failure, if the stream is accessed further then the behavior remains undefined. But it is a good programming habit to close any file once it is no longer needed. This helps in better utilization of system resources, and is very useful when you are working on numerous files simultaneously. Some operating systems place a limit on the number of files that can be open at any given point in time.

Read from & Write to files

Once a file is open, we can read from it or write into to it in following ways –

Character Input and Output – fgetc() and fputc()

fgetc() – Function fgetc() reads a single character from the file which has previously been opened using a function like fopen().

Syntax:

fp – file pointer

fputc() – Functionf putc() does the opposite, it writes a character to the file identified by its second argument.

Syntax:

The second argument in the putc() function must be a file opened in either write or append mode.

Example:

Formatted Input Output – fprintf() and fscanf()

fprintf – The fprintf() function is used for formatted write to file. It returns number of character printed or negative number on error.

Syntax:

fscanf– The fscanf() function is used for formatted read from file. It returns number of values read successfully.

Syntax:

Example:

Binary File Input Output – fread() and fwrite()

fread() – It reads from binary file.

Syntax:

buffer – stores the value read, size – size of the buffer, num – number of blocks to be read, ptr – file pointer

If it encounters an error or end-of-file, it returns a zero, you have to use feof() or ferror() to distinguish between these two.

fwrite() – It write to a binary file.

Syntax:

buffer – stores the value read, size – size of the buffer, num – number of blocks to be read, ptr – file pointer

It returns the number of items written to ptr.

fseek() – It moves the file pointer by given offset.

Syntax:

ptr – file pointer, offset – offset in bytes from third parameter

whence(SEEK_SET – from beginning of file,SEEK_CUR – from current position,SEEK_END – from end of file)

It returns a zero on success and non zero on failure.

ftell() – It tells the position of file pointer.

syntax:

ptr – FILE pointer

It returns position of pointer on success or -1 on error.

Example:

rewind() – It sets the file pointer to the beginning of the file.

Syntax:

ptr – FILE pointer

Example:

EOF (The End of File Marker)

EOF is a character which indicates the end of a file. It is returned by read commands when they try to read beyond the end of a file.