File Structure in C

September 30, 2014 0 Comments

The file meant for a C programmer is nothing but a data file. Using C programs the data in the form of character or characters is stored permanently on the secondary storage device. In file handling such programs are designed that store data in file and read back data from the file. It means the file replaces the input/output devices for I/O operations. Rather than displaying the data on the output device monitor, it can be written to the file or the data can be read from the file instead of the input device like keyboard. Contents of a file describe the structure of a file.

The way how the contents of a file are being managed or decided is called as file Structure. It may be character, fields, records or block of records.

Each and every data stored in a file can be treated simply as character. So, the character is simplest unit of transfer. In its simplest structure a file can contain a character. More number of characters can be stored in file to make it meaningful. If the transfer rate is simply a character then there is lots of overhead. In order to overcome this overhead the characters can be grouped into many numbers of characters. A file with ‘character’ structure looks as follows:

Twinkle twinkle little star!
How I wonder what you are?
Up above the world so high!
Like a diamond in the sky.

In this case of the file ‘POEM.TXT’ each time a single character is written in the file. The file is simply a collection of cnaracters. So, the structure of the file is 'character'. [A text file]
The grouped characters can be transferred to or from the file. In such case a ‘field' can be treated as unit of transfer. The field here is nothing but a variable of any standard data type. So, the file can now contain these fields in its structure. The length of the field varies according to the contents in it. It is not fixed. So, the random access is not possible. The field values must be separated by a marker indicating end of the value. The file creator must take care in such cases.

A file with ‘field’ structure looks as follows:

101 jacob 102 jacob2
103 bill  104  bill2
105 jacob3 106  bill3

In this case of the file ‘STUDENT.DAT’ each time the data from variables is transferred. Two variables like 'rollno' and 'name’ are used in this case. The file is a collection of data in the form of characters only. But the structure of the file is ‘field’.

The fields that logically explain the characteristics of a person or a thing can be grouped in the form of 'records’ and  these records can be stored in the file. The file is now a collection of records.So, the unit of transfer in this case will be a record. A whole record can be transferred at a time to or from the file. So, in this case the structure of a file can have records in it. Generally here the file contains fixed length records. Number of bytes transferred is equal to the length of record. So, a record containing many fields is transferred, individual fields.

A file with ‘record’ structure looks as follows:


101   A    25400
10    B    3240
13    C    12500
104   D    5400

In this case of the file ‘EMP.DAT’ each time the data from a complete record is transferred. For the sake of understanding each record is written in new line. The record structure consists of three fields (like ‘empno’, ‘empname’ and ‘salary’) are used in this case. The file is a collection of data in the form of records only. But the length of the record is fixed. So, there is chance of wastage of storage space. But random access or direct access is the great utilization of fixed length ‘record structure’.

 Further the records can be grouped into ‘blocks’. A block may contain ‘n’ number of records. In such case the data transfer rate is in terms of ‘block’. A block can be transferred at a time. So, the structure of file is ‘block’ of records.

 In order to support such files the definition of the control structure for streams defined in C which as “FILE structure” is defined as follows:

typedef struct {

int level;     /* Full or empty level of buffer*/
unsigned flags;    /* File status flags */
char fd;           /* File descriptor (handler) */
unsigned char hold; /* Used to indicate no buffer */
int bsize;           /* Buffer size */
unsigned char *buffer;  /* Data transfer buffer */
unsigned char *curp;  /* Current active pointer */
unsigned istemp;  /* Temporary file indicator*/
short token;      /* Used for validity checking*/


FILE is the structured data type name using which the logical file pointers are declared in C programs. Whenever a file is to be operated for input or output operations the pointer variable of the type FILE is used. We discuss such FILE pointer in the next article in detail.

Jacob Lefore

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google