Quick viewing(Text Mode)

Types of File in C File Handling

Types of File in C File Handling

Types of in File handling

A file represents a sequence of on the disk where a group of related is stored. File is created for permanent storage of data. It is a readymade structure.

We can store data into two different files:

Text Files

Text files are special subset of binary files that are used to store human readable characters as a rich text document or document. Text files also store data in sequential bytes but in represents characters.

Text files are less prone to get corrupted as any undesired change may just show up once the file is opened and then can easily be removed.

Text files are of two types:

. Plain text files: These files store End of Line (EOL) marker at the end of each line to represent line break and an End of File (EOF) at the end of the file to represent end of file.

. Rich text files: These files also follow the same schema as the plain text files but may also store text related information like text colour, text style, font style etc.

Because of simple and standard format to store data, text files are one of the most used file formats for storing textual data and are supported in many applications.

Binary File

Binary file are those typical files that store data in the form of sequence of bytes grouped into eight bits or sometimes sixteen bits. These bits represent custom data and such files can store multiple types of data (images, audio, text, etc) under a single file. Binary file can have custom file formats and the developer, who designs these custom file formats, converts the information, to be stored, in bits and arranges these bits in binary file so that they are well understood by the supporting application and when needed, can easily be by the supporting application.

One most common example of binary file is image file is .PNG or .JPG. If one tries these files using a then, he/she may get unrecognizable characters, but when opened using the supporting image viewer, the file will be shown as a single image. This is because the file is in binary format and contains data in the form of sequence of bytes. When the text editor tries to read these bytes and tries to convert bits into characters, they get undesired special characters and display it to the user.

Binary files also store file information like file name, , etc., which may be included in the file as header to the file and is visible even when the file is opened in a text editor.

Difference between text file and binary file There are two kinds of storing data in file text format and binary format .

1. In text file data are stored as line of characters with each line terminated by a new line (‘\n’). In binary format data is stored on the disk in the same way as it is represented in the computer memory . system does not make any distinction between text file and binary file. 2. Text file are in human readable form and they can be created and read using any text editor , while binary file are not in human readable form and they can be created and read only by specific program written for them .

3. The binary data stored in file can not be read using text editor. The hexadecimal value of 1679 is 0x068f , so in binary format it is represented by two bytes 0x06 and 0x8f . In a text file 1679 is represented by the bytes 0x31 , 0x36 , 0x37 ,0x39 ( ASCII values) . NOTE ASCII value for 1 is 31 and for 9 is 39. 4. Both text file and binary file keep record of the length of the file , and identify the end of file when this is reached . In text files , there is one more way to detect the end of file. The character with ASCII value 26 is considered to be end of file character in text files . All input function stop reading character when this character is encountered and return end of file signal to the program . C does not insert this character in file , it can be entered through the keyboard by Ctrl + Z (Ctrl + D on some systems) . In binary files no such character represent end of file. 5. In text files is stored as a combination of carriage return ‘\r’ (ASCII 13) and linefeed ‘\n’(ASCII 10) while in binary files newline is stored only as ‘\n’(ASCII 10). 6. In binary format , the data is stored in the same way as it is represented in memory so no conversion s have to take place while transferring of data between memory and file . In text format some conversions 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 combination of(‘\r’) and (‘\n’) and while reading from text file this combination (‘\r’) and (‘\n’) is to be converted back (‘\n’). 7. The input and output operations in the binary files takes less time as compared to the text files because no conversion has to be take place . However the data written using binary is not very portable because size of and order may be different on different machines . In text format , these problems do not raise so it is considered more portable

Difference between Text File and Binary File (Summary)

Text File Binary File

Bits represent character. Bits represent a custom data.

Less prone to get corrupt as changes reflect as soon as the file is opened and can easily Can easily get corrupted, even a single be undone. change may corrupt the file.

Can store different types of data (image, audio, Can store only plain text in a file. text) in a single file.

Widely used file format and can be opened Developed especially for an application and using any simple text editor. may not be understood by other applications.

Mostly .txt and .rtf are used as extensions to text files. Can have any application defined extension.