Session 12

| File Handling Session 12 Objectives Explain streams and files Discuss text streams and binary streams Explain the various file functions Explain file pointer Discuss current active pointer Explain command-line arguments File Input/Output All I/O operations in C are carried out using functions from the standard library This approach makes the C file system very powerful and flexible I/O in C is unique because data may be transferred in its internal binary representation or in a human-readable text format Streams The C file system works with a wide variety of devices including printers, disk drives, tape drives and terminals Though all these devices are very different from each other, the buffered file system transforms each device into a logical device called a stream Since all streams act similarly, it is easy to handle the different devices There are two types of streams - the text and binary streams Text Streams A text stream is a sequence of characters that can be organized into lines terminated by a new line character In a text stream, certain character translations may occur as required by the environment Therefore, there may not be a one-to-one relationship between the characters that are written (or read) and those in the external device Also, because of possible translations, the number of characters written (or read) may not be the same as those in the external device Binary Streams A binary stream is a sequence of bytes with a one-to-one correspondence to those in the external device, that is, there are no character translations The number of bytes written (or read) is the same as the number on the external device Binary streams are a flat sequence of bytes, which do not have any flags to indicate the end of file or end of record The end of file is determined by the size of the file Files A file can refer to anything from a disk file to a terminal or a printer A file is associated with a stream by performing an open operation and disassociated by a close