C programming files I/O

In this tutorial, you will learn about c programming files IO operations and functions associated with it.

c programming files I/O

C programming files IO operations and functions

So far we have used i/o functions like printf and scanf to write and read data. These are console oriented functions used when data is small.

So for handling large data, a new approach is introduced in c programming called file handling where data is stored on the local machine and can be operated without destroying any data.

A file simply is a collection of data stored in the form of a sequence of bytes in a machine.

Before jumping onto file handling functions we must know about the types of files used in c programming language.

  • Text File
  • Binary File

Text files in C

Text files are humanly readable which is displayed in a sequence of characters that can be easily interpreted by humans as it presented in the textual form. Common editors like notepad and others can be used to interpret and edit them.

They can be stored in plain text (.txt) format or rich text format (.rtf).

Binary files in C

In binary files data is displayed in some encoded format (using 0’s and 1’s) instead of plain characters. Typically they contain the sequence of bytes.

They are stored in .bin format.

Thus C supports some functions to handle files and performs some operations. It includes:

  • opening a file
  • reading data from a file
  • writing data to a file
  • naming a file
  • closing a file

File handling in C

For any file handling operations first, a pointer of FILE type must be declared in C.
For example:

FILE *fp;

[adsense1]

Common I/O functions used for file handling in C

Functions
fopen()
fclose()
getc()
putc()
fprintf()
fscanf()
getw()
putw()
fseek()
ftell()
rewind()
Operations
For creating new file or opening existing file
For closing a file
For reading a character from file
For writing a character to a file
For writing set of data to a file
For reading set of data from a file
For reading an integer from a file
For writing an integer to a file
For setting the position to the desired point in a file
Gives the current position in a file
Sets the position to the beginning of the file

Opening a file

As mentioned in above table fopen() is a standard function used to create a new file or open an existing file from our local machine.
The general format for declaring and opening a file is:

FILE *ptr;
fp = fopen("filename","mode");

First, a pointer fp of FILE type is declared. In the second statement, fopen function is used to open the file named filename with a purpose which is defined by the mode.

The file is assigned to pointer variable fp which is used as the communication link between the system and the program.

The purpose of opening a file is defined by mode. It can be any of the following:

File opening modes in standard file I/O

Mode
r
w
a
r+
w+
a+
rb
wb
ab
rb+
wb+
ab+
Purpose
opens a text file in reading mode
opens the text file for writing mode
opens the text file for appending mode or adding data to it
opens the text file for both reading and writing mode
opens the text file for both reading and writing mode
opens the text file for both reading and writing mode
opens a binary file in reading mode
opens a binary file in writing mode
opens the binary file for appending mode or adding data to it
opens the binary file for both reading and writing mode
opens the binary file for both reading and writing mode
opens the binary file for both reading and writing mode

For example:

FILE *fp;
fp = fopen("C://myfile.txt","r");

Here in this example, ‘myfile.txt’ will be opened in reading mode and if the file doesn’t exist in the specified location, it will return NULL.

NOTE: if the mode is writing mode then a new file will be created if the file doesn’t exist.

Closing a file

A file must be closed once it is opened and all operations are done because of various reasons like it will prevent any accidental misuse of the file. Moreover, sometimes the number of files that can be opened simultaneously is limited. So it is always best to close files once all operations are finished.

fclose() is used for closing a file.

fclose(fp); // fp was the pointer to which our previously opened file was assigned