Awefea
Awefea
Awefea
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
Elementary Programming with C/Session 12/ 2 of 28
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
Elementary Programming with C/Session 12/ 3 of 28
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
Elementary Programming with C/Session 12/ 4 of 28
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
Elementary Programming with C/Session 12/ 5 of 28
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
Elementary Programming with C/Session 12/ 6 of 28
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 operation When a program terminates normally, all files are automatically closed When a program crashes, the files remain open
feof( )
ferror( ) rewind( ) remove( ) fflush( )
File Pointer
A file pointer is essential for reading or writing files It is a pointer to a structure that contains the file name, current position of the file, whether the file is being read or written, and whether any errors or the end of the file have occurred The definitions obtained from stdio.h include a structure declaration called FILE The only declaration needed for a file pointer is:
FILE *fp
a
r+ w+ a+f
String l/O
The functions fputs() and fgets() write and read character strings to and from a disk file The fputs() function writes the entire string to the specified stream The fgets() function reads a string from the specified stream until either a new line character is read or length-1 characters have been read The prototypes are:
int fputs(const char *str, FILE *fp); char *fgets( char *str, int length, FILE *fp);
Elementary Programming with C/Session 12/ 14 of 28
Opening a File-Binary
The fopen() function opens a stream for use and links a file with that stream The fopen() function returns a file pointer associated with the file The prototype for the fopen() function is: FILE *fopen(const char *filename, const char *mode);
Mode rb wb Meaning Open a binary file for reading Create a binary file for writing
ab
r+b w+b a+b
Using feof()
The function feof() returns true if the end of the file has been reached, otherwise it returns false (0) This function is used while reading binary data The prototype is: int feof (FILE *fp);
Elementary Programming with C/Session 12/ 18 of 28
Erasing Files
The remove() function erases a specified file Its prototype is: int remove(char *filename);
Flushing streams
The fflush() function flushes out the buffer depending upon the file type A file opened for read will have its input buffer cleared, while a file opened for write will have its output buffer written to the files Its prototype is:
int fflush(FILE *fp); The fflush() function, with a null, flushes all files opened for output
Elementary Programming with C/Session 12/ 22 of 28
Origin
File Location
SEEK_SET or 0 SEEK_CUR or 1
SEEK_END or 2