Chapter 16 Files
Chapter 16 Files
FILE HANDLING IN C
STREAMS IN C
• In C, the standard streams are termed as pre-connected input and output channels between a text
terminal and the program (when it begins execution). Therefore, stream is a logical interface to the
devices that are connected to the computer.
• Stream is widely used as a logical interface to a file where a file can refer to a disk file, the computer
screen, keyboard, etc. Although files may differ in the form and capabilities, all streams are the same.
• The three standard streams in C languages are- standard input (stdin), standard output (stdout) and
standard error (stderr).
• Standard input (stdin): Standard input is the stream from which the program
receives its data. The program requests transfer of data using the read
operation. However, not all programs require input. Generally, unless
redirected, input for a program is expected from the keyboard.
KEYBOARD
• Standard output (stdout): Standard output is the stream where a program
stdin
writes its output data. The program requests data transfer using the write
PROGRAM
operation. However, not all programs generate output.
stderr
• Standard error (stderr): Standard error is basically an output stream used by
SCREEN
programs to report error messages or diagnostics. It is a stream independent
stdout
of standard output and can be redirected separately. No doubt, the standard
output and standard error can also be directed to the same destination.
• A stream is linked to a file using an open operation and disassociated from a
file using a close operation.
PROGRAM
BUFFER DISK
Similarly, when reading data from a disk file, the data is read as a block from the file and written into the
buffer. The program reads data from the buffer. The creation and operation of the buffer is automatically
handled by the operating system. However, C provides some functions for buffer manipulation. The data
resides in the buffer until the buffer is flushed or written to a file.
MODE DESCRIPTION
r Open a text file for reading. If the stream (file) does not exist then an error will be reported.
Open a text file for writing. If the stream does not exist then it is created otherwise if the file already exists, then its
w
contents would be deleted
a Append to a text file. if the file does not exist, it is created.
rb Open a binary file for reading. B indicates binary. By default this will be a sequential file in Media 4 format
wb Open a binary file for writing
ab Append to a binary file
Open a text file for both reading and writing. The stream will be positioned at the beginning of the file. When you
r+
specify "r+", you indicate that you want to read the file before you write to it. Thus the file must already exist.
Open a text file for both reading and writing. The stream will be created if it does not exist, and will be truncated if
w+
it exist.
a+ Open a text file for both reading and writing. The stream will be positioned at the end of the file content.
r+b/ rb+ Open a binary file for read/write
w+b/wb+ Create a binary file for read/write
a+b/ab+ Append a binary file for read/write
FILE *fp;
fp = fopen("Student.DAT", "r");
if(fp==NULL)
{
printf("\n The file could not be opened");
exit(1);
}
OR
char filename[30];
FILE *fp;
gets(filename);
fp = fopen(filename, "r+");
if(fp==NULL)
{
printf("\n The file could not be opened");
exit(1);
}
fscanf()
The fscanf() is used to read formatted data from the stream. The syntax of the fscanf() can be given as,
• int fscanf(FILE *stream, const char *format,…);
• The fscanf() is used to read data from the stream and store them according to the parameter format into the locations
pointed by the additional arguments.
#include<stdio.h>
main()
{ FILE *fp;
char name[80];
int roll_no;
fp = fopen("Student.DAT", "r");
if(fp==NULL)
{ printf("\n The file could not be opened");
exit(1);
}
printf("\n Enter the name and roll number of the student : ");
fscanf(stdin, "%s %d", name, &roll_no); /* read from keyboard */
• fwrite() can be used to write characters, integers, structures, etc to a file. However, fwrite() can be used
only with files that are opened in binary mode.
20
© Oxford University Press 2016. All rights reserved.