Skip to main content

File Structure in C

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.


Popular posts from this blog

difference between structure and union in C Language

In c language article we will see the difference between union and structure. Both are the user define datatype in c language. See the table which is mentioned below: ASP.NET Video Tutorial Series Structure Union1.The keywordstruct is used to define a structure 1. The keyword union is used to define a union. 2. When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum ofsizes of its members. The smaller members may end with unused slack bytes. 2. When a variable is associated with a union, thecompiler allocates thememory by considering the size of the largest memory. So, size of union is equal to the size of largest member. 3. Each member within a structure is assigned unique storage area of location. 3. Memory allocated is shared by individual members of union. 4. The address of each member will be in ascending order This indicates that memory for each member will start at different offset v…

Difference between Linear search and Binary Search in c language

SQL Video Channel : Download all SQL Video

Binary Search Linear Search Works only on sorted items. such as  1,2,3,4,5,6  etc
Works on sorted as well as unsorted items. 12,4,5,3,2,1 etc Very efficient if the items are sorted Very efficient if the items are less and present in the beginning of the list. such as Suppose your list items are : 12,3,4,5,1 and you want to search 12 number then you get beginning in the list. Works well with arrays and not on linked lists. Works with arrays and linked lists.
Number of comparisons are less More number of comparisons are required if the items are present in the later part of the array or its elements are more.

Memory representation of Linked List Data Structures in C Language

Memory representation of Linked List

             In memory the linked list is stored in scattered cells (locations).The memory for each node is allocated dynamically means as and when required. So the Linked List can increase as per the user wish and the size is not fixed, it can vary.

               Suppose first node of linked list is allocated with an address 1008. Its graphical representation looks like the figure shown below:

      Suppose next node is allocated at an address 506, so the list becomes,

  Suppose next node is allocated with an address with an address 10,s the list become,

The other way to represent the linked list is as shown below:

 In the above representation the data stored in the linked list is “INDIA”, the information part of each node contains one character. The external pointer root points to first node’s address 1005. The link part of the node containing information I contains 1007, the address of next node. The last node …