Skip to main content

File types, Text and Binary in C

As discussed earlier the C language provides flexibility for the programmers to write file handling programs to store and process data. Such data files contain the data effectively in two formats. So, on the basis of the format structure of the files are of two types, Text File and Binary File.

Text File

In a text file a stream of characters are stored sequentially without any formatting. So, it is very simple to handle but direct access is not possible because any value or character is not stored after fixed number of bytes. Even though the numbers are stored in the form of characters only and the record length is not fixed if the records are stored. Again the records are also stored in the character format of the individual fields.

Similarly, since text files only process characters, generally they can only read or write data one character at a time. But the flexible C Language provides file handling functions to read and write formatted data and lines of text in a text file. But these functions again basically process data one character at a time. The normal functions used to handle the text file are getc(),fgetc(),fgets(), fputs(), putc(),fputc(),fscanf() and fprintf(). These functions transfer the character/s to and from the text file.

Depending on the requirements of the OS (Operating System), newline character may be converted to or from carriage-return/linefeed combination depending on the data is being written to, or read from, the file. Other character conversions may also occur to satisfy the storage requirements of the OS. These translations occur transparently and they occur while processing a text file.

Binary File

A binary file is not much different to a text file. It is also a collection of bytes. But the transfer of data is purely in the form of bytes as stored in main memory. Any number of bytes can be transferred at a time, in binary file. But the length of the bytes transferred is fixed. So, the binary files contain the data in fixed length record format. That is the reason they are more effective than text files. One important advantage of fixed length is that of accessing the record directly. The direct access is possible only in case of the binary files. It is basically because when a record is written to a binary file form the memory, the complete memory block is written. The number of bytes transferred is fixed. In the same manner when the data is read from the binary file the number of bytes is transferred to the memory at the provided address. The file handling functions provided to handle the binary files are fread() and fwrite(). This fixed length transfer is advantageous for direct access but the size of the file increases. So, in comparison to text files the binary files are advantageous in direct access at the cost of the size. Thus a selection of type of file is decided keeping the accessibility of data in mind.

A binary file is also referred to as a character stream but differ in the following ways:

• No special processing of the data occurs and each byte of data is transferred unprocessed to or from the disk.

• C Language places no constraints on the file, and it may be read from, or written to, in any manner.

Binary files can be either processed sequentially or randomly. The processing of a binary file using random access techniques involves moving the file pointer position to an appropriate place in the file before reading or writing data. This indicates a second characteristic of binary files; the binary files are generally processed using read and write operations simultaneously. For example, for the creation and processing a database file the best choice is a binary file. A record update operation will involve locating the appropriate record, reading the record into memory, modifying it in some way, and finally writing the record back to disk at its appropriate location in the file. These kinds of operations are common to many binary files, but are rarely found in applications that process text files.

The type of the file to be processed is decided at the time of opening a file. At the time of opening a file the mode of operation is mentioned indicating text file or binary file. The function fopen() provides the capability of opening a file. By default the file is opened in text mode. If the binary file is to be processed then it is explicitly defined as “b” along with the other required mode character ‘r’, ‘w’ or ‘a’.

When the file is opened the mode of operation is specified. It indicates the types of files “text” and "binary". The text file is advantageous in terms of size and the binary file in terms of random or direct access.

Comments

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 …