Professional Documents
Culture Documents
47 Files
47 Files
Week9
File Handling
Files
Files
• What is a file?
– Collection of bytes stored on secondary storage like hard disks.
Files
• What is a file?
– Collection of bytes stored on secondary storage like hard disks.
We can't define anything precisely. If we attempt to, we get into that paralysis of thought that comes to
philosophers… one saying to the other: "you don't know what you are talking about!". The second one says: "what
do you mean by talking? What do you mean by you? What do you mean by know?"
– Feynman, Lectures on Physics, Vol 1, Chapter 8
File Access
File Access
• Recall (long back): we remarked that 3
files are always connected to the program :
File Access
• Recall (long back): we remarked that 3
files are always connected to the program :
– stdin : the standard input, from where scanf,
getchar(), gets() etc. read input from (usually
has file descriptor 0)
File Access
• Recall (long back): we remarked that 3
files are always connected to the program :
– stdin : the standard input, from where scanf,
getchar(), gets() etc. read input from (usually
has file descriptor 0)
– stdout : the standard output, to where
printf(), putchar(), puts() etc. output to. (file
descriptor 1)
File Access
• Recall (long back): we remarked that 3
files are always connected to the program :
– stdin : the standard input, from where scanf,
getchar(), gets() etc. read input from (usually
has file descriptor 0)
– stdout : the standard output, to where
printf(), putchar(), puts() etc. output to. (file
descriptor 1)
– stderr : standard error console. (file
descriptor 2)
Reading input using the standard
file descriptors
Reading input using the standard
file descriptors
• $./a.out < inputfile
– This will use inputfile as the source, instead of
the keyboard
Reading input using the standard
file descriptors
• $./a.out < inputfile
– This will use inputfile as the source, instead of
the keyboard
• $./a.out 2>errfile
– This will redirect error messages to errfile,
instead of stderr.
Some usage scenarios
Some usage scenarios
• What if we want to read input from multiple
files, and redirect output to multiple files in
the program?
Some usage scenarios
• What if we want to read input from multiple
files, and redirect output to multiple files in
the program?
Compare with
scanf and printf
– first argument
fp is missing
General Scheme of File handling in C
Compare with
scanf and printf
– first argument
fp is missing
General Scheme of File handling in C
Compare with
scanf and printf
– first argument
fp is missing
General Scheme of File handling in C
– “r” : read-only mode (Any write to the file will fail). File must
exist.
Opening Files
• FILE* fopen (char *name, char *mode)
• The first argument to fopen is the name of the file – can be
given in short form (e.g. “inputfile”) or the full path name (e.g.
“/home/don/inputfile”)
• The second argument is the mode in which we want to open the
file. Common modes include:
– “r” : read-only mode (Any write to the file will fail). File must
exist.
– “w” : write mode. The first write happens at the beginning of
the file, by default. Thus, may overwrite the current
content. A new file is created if it does not exist.
Opening Files
• FILE* fopen (char *name, char *mode)
• The first argument to fopen is the name of the file – can be
given in short form (e.g. “inputfile”) or the full path name (e.g.
“/home/don/inputfile”)
• The second argument is the mode in which we want to open the
file. Common modes include:
– “r” : read-only mode (Any write to the file will fail). File must
exist.
– “w” : write mode. The first write happens at the beginning of
the file, by default. Thus, may overwrite the current
content. A new file is created if it does not exist.
– “a” : append mode. The first write is to the end of the
current content. File is created if it does not exist.
Opening Files
• If successful, fopen returns a file
pointer – this is later used for fprintf,
fscanf etc.
“The instructor of this course owns the copyright of all the course materials. This lecture material
was distributed only to the students attending the course ESC-101 of IIT Kanpur, and should not
be distributed in print or through electronic media without the consent of the instructor. Students
can make their own copies of the course materials for their use.”