Share this post on:

Files in C Language, Input and output operations that we have performed so far were done through keyboard and screen only. The data was input through keyboard and was output on the screen. After termination of program, all the entered data is lost because primary memory is volatile. If the data has to be used later, then it becomes necessary to keep it in permanent storage device. There are many I/O library functions through which data can be stored on the disk or any secondary storage device. The stored data can be read whenever required. A data file is a collection or related data placed on a secondary storage device.

File handling in C can be broadly categorized in two types –

  • High-level Files (standard files or stream oriented files)
  • Low-level files (system oriented files)

High level file handling is managed by library functions while low level file handling is managed by system calls. The high file handling is commonly used since it is easier to managed and hides most of the details from the programmer.

The header file stdio.h should be included in the program to make use of the I/O function. We have already performed I/O with screen and keyboard using function like scanf(), printf(), gets(), puts(), getchar() and putchar(). The advantage of using stream oriented file I/O is that the I/O in files is similar to the screen, keyboard I/O. for example we have functions like fscanf(), fprintf(), fgets(), fputs() .

Text and Binary Streams

Input and output in C is performed with the help of logical data streams, which can be connected to different input- output devices. The concept of streams makes input and output on different devices uniform. The programmer is not concerned about the different properties of these devices, all of them can be handled in a similar way. File management is done by the execution environment and from environment, and streams are channels through which data can flow from program to environment and from environment to program. A stream is associated with a file by opening the file and is disassociated with the file by closing the file. There are two types of stream text stream and binary streams. Text stream is a stream is a characters and binary stream is a stream of unprocessed bytes.

The text stream is a sequence of character, arranged in lines where each line consists of zero or more characters and it terminated by newline by a newline character. In binary streams data is represented as it is coded internally in the computer’s main memory, without any modifications. Unix system does not make any distinction between text file and binary files.

Text files are in human readable form and they can be created and read using any text editor, while binary files are not in human readable form and they can be created and read only by specific programs written for them. The binary data stored in the file can’t be read using a text editor. The integer 167985 will take only 4 bytes in a binary file while it will occupy 6 bytes in a text file because in binary file it is stored as an integer while in text file is stored as a sequence of 5 characters that is ‘1’, ‘6’, ‘7’, ‘9’, ‘8’, ‘5’.

Both text and file and binary files keep record of the files, and identify the end of file when this length is reached. In text files, there is one more way to detect the end of file.

The binary format, data is stored in the same way as it is represented in memory so no conversion have to take place while transferring of data between memory and file. for example while writing to a text file newline(\n) has to be converted to underlying system’s end of line representation and while reading from a text file this end of line representation is converted to ‘\n’. All these conversion are done by the library functions.

Buffer

Buffer is an area in memory where the data is temporarily stored before being written to the file. when we open a file, a buffer is automatically associated associated with its file pointer. Whenever data we send file is not immediately written to the file. First it is sent to the buffer and when the buffer is full, its contents are written to the file. When the file is closed, all the contents or buffer, we can also explicitly flush the buffer by a function fflush().

The concept of buffer is used to increase efficiency. Had there been no buffer we would have to access the disk each time for writing even single byte of data. This would have taken lot of time because each time the disk is accessed, the read\write head has to be repositioned. When buffering is done, the data is collected in the buffer and data equal to the size of buffer is written to the file at a time, so the number of times disk is accessed decreases, which improves the efficiency.

The steps for file operations in C programming are-

  1. Open a file
  2. Read the file or write data in the file
  3. Close the file

Opening a File

A file must be opened before any I/O operations can be performed on that file. The process of establishing a connection between the program and file is called opening the file. When a file is opened, a stream is associated with that file, a buffer is created in the main memory for transferring data to and from the file.

A structure named FILE is defined in the header file stdio.h that contains all information about the file like name, status (read, write etc), buffer location, current position in the buffer, end of file status, error status etc. All these details are hidden from the programmer and the operating system take care of all these things.

A file pointer(stream pointer is pointer) to a structure of type FILE. whenever a file is opened, a structure of type FILE is associated with it, and a file pointer that points to this structure identifies this file. The function fopen() is used to open a file.

Declartion : FILE *fopen (const char *filename, const char * mode);

Error in Opening Files

If an error occurs in opening a file, then fopen() returns NULL. So we can check for any errors in opening by checking the return value of fopen().

Error is opening a file may occurs due to various reasons, for example-

  1. If we try to open a file in read or update mode, and file doesn’t exist or we do not have read permission on that file.
  2. If we try to create a file but there is not space on the disk or we don’t have written permission
  3. If we try to create a file that already exists.
  4. Operating system limits the number of files that can be opened at a time and we trying to open more files that that number.

Closing a File

The file that was opened using fopen() function must be closed when no more operations are to be performed on it.

Declaration : int fclose(File *fptr);

After closing the file, connection between file pointer and the file is broken. Now the file pointer fptr is free to be connected to some other file. On closing the file, the buffer associated with it is flushed that all the data that is in the buffer is written to the file. The buffers allocated by the systems for the file are freed after the file is closed, so that these buffers can be available for other files. Some systems have a limitation on the number of files that can be opened at a time, so we should close the file that are not currently in use so that we can open other files.

Share this post on:
Avatar Raiyan

Author: Raiyan

Hi, I guess you're here because you want to know a bit about me. huh? I am not so good on talking about myself but I'll give a short about me.

My Name is Raiyan. I am a Professional Application Developer and a Blogger.
I started this website to Share my Knowledge. Here I provide all my knowledge whatever I earned till now.

Leave a Comment

Your email address will not be published. Required fields are marked *