Download as pdf or txt
Download as pdf or txt
You are on page 1of 53

File Handling

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.

• Any addressable part of the file system in an Operating system can be


a file. (includes such strange things as /dev/null (nothing), /dev/urand
(random data device), /dev/audio (speakers), and of course, our data
files like /home/don2/input.txt)
Files
• What is a file?
– Collection of bytes stored on secondary storage like hard disks.

• Any addressable part of the file system in an Operating system can be


a file. (includes such strange things as /dev/null (nothing), /dev/urand
(random data device), /dev/audio (speakers), and of course, our data
files like /home/don2/input.txt)

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 > outfile


– This will use outfile as the output, instead of the
terminal
Reading input using the standard
file descriptors
• $./a.out < inputfile
– This will use inputfile as the source, instead of
the keyboard

• $./a.out > outfile


– This will use outfile as the output, instead of the
terminal

• $./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?

• The redirection mechanism is provided by the


Linux shell, and is not a part of the C
programming language
Some usage scenarios
• What if we want to read input from multiple
files, and redirect output to multiple files in
the program?

• The redirection mechanism is provided by the


Linux shell, and is not a part of the C
programming language

• Does C provide any functions to handle files?


General Scheme of File handling in C

Compare with
scanf and printf
– first argument
fp is missing
General Scheme of File handling in C

1. Open the file for reading/writing etc. – this


will return a file pointer – this points to a
structure containing information about the
file: location of a file, the current position
being read in the file, and so on.
– FILE* fopen (char *name, char *mode)

Compare with
scanf and printf
– first argument
fp is missing
General Scheme of File handling in C

1. Open the file for reading/writing etc. – this


will return a file pointer – this points to a
structure containing information about the
file: location of a file, the current position
being read in the file, and so on.
– FILE* fopen (char *name, char *mode)
2. Read/Write to the file
– int fscanf(FILE *fp, char *format, …)
– int fprintf(FILE *fp, char *format, …)

Compare with
scanf and printf
– first argument
fp is missing
General Scheme of File handling in C

1. Open the file for reading/writing etc. – this


will return a file pointer – this points to a
structure containing information about the
file: location of a file, the current position
being read in the file, and so on.
– FILE* fopen (char *name, char *mode)
2. Read/Write to the file
– int fscanf(FILE *fp, char *format, …)
– int fprintf(FILE *fp, char *format, …)
3. Close the File. Compare with
– int fclose(FILE *fp) scanf and printf
– first argument
fp is missing
• Write a program that will take two
filenames, and print contents to the
standard output. The contents of the first
file should be printed first, and then the
contents of the second.
• The algorithm:
• Write a program that will take two
filenames, and print contents to the
standard output. The contents of the first
file should be printed first, and then the
contents of the second.
• The algorithm:
1. Read the file names.
• Write a program that will take two
filenames, and print contents to the
standard output. The contents of the first
file should be printed first, and then the
contents of the second.
• The algorithm:
1. Read the file names.
2. Open file 1. If open failed, we exit
• Write a program that will take two
filenames, and print contents to the
standard output. The contents of the first
file should be printed first, and then the
contents of the second.
• The algorithm:
1. Read the file names.
2. Open file 1. If open failed, we exit
3. Print the contents of file 1 to stdout
• Write a program that will take two
filenames, and print contents to the
standard output. The contents of the first
file should be printed first, and then the
contents of the second.
• The algorithm:
1. Read the file names.
2. Open file 1. If open failed, we exit
3. Print the contents of file 1 to stdout
4. Close file 1
• Write a program that will take two
filenames, and print contents to the
standard output. The contents of the first
file should be printed first, and then the
contents of the second.
• The algorithm:
1. Read the file names.
2. Open file 1. If open failed, we exit
3. Print the contents of file 1 to stdout
4. Close file 1
5. Open file 2. If open failed, we exit
• Write a program that will take two
filenames, and print contents to the
standard output. The contents of the first
file should be printed first, and then the
contents of the second.
• The algorithm:
1. Read the file names.
2. Open file 1. If open failed, we exit
3. Print the contents of file 1 to stdout
4. Close file 1
5. Open file 2. If open failed, we exit
6. Print the contents of file 2 to stdout
• Write a program that will take two
filenames, and print contents to the
standard output. The contents of the first
file should be printed first, and then the
contents of the second.
• The algorithm:
1. Read the file names.
2. Open file 1. If open failed, we exit
3. Print the contents of file 1 to stdout
4. Close file 1
5. Open file 2. If open failed, we exit
6. Print the contents of file 2 to stdout
7. Close file 2
Opening Files
Opening Files
• FILE* fopen (char *name, char *mode)
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”)
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:
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.
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.

• If unsuccessful, fopen returns a NULL.

• It is a good idea to check for errors


(e.g. Opening a file on a CDROM using
“w” mode etc.)
The Program
int main()
{
FILE *fp1, *fp2;
char filename1[128], filename2[128];
gets(filename1);
gets(filename2);
if( ( fp1 = fopen( filename1, "r" ) ) == NULL ) {
fprintf( stderr, "Opening File %s failed\n",
filename1 );
return;
}
copy_file( fp1, stdout );
fclose( fp1 );
if ( ( fp2 = fopen ( filename2, "r" ) ) == NULL ) {
fprintf ( stderr, "Opening File %s failed\n",
filename2);
return;
}
copy_file ( fp2, stdout );
fclose ( fp2 );
return 0;
}
copy_file function

void copy_file ( FILE *fromfp, FILE *tofp )


{
char c;

while ( !feof ( fromfp ) ) {


fscanf ( fromfp, "%c", &c );
fprintf ( tofp, "%c", c );
}
}
Some other file handling
functions
• int feof ( FILE* fp );
– Checks whether the EOF is set for fp – that is, the EOF has
been encountered. If EOF is set, it returns nonzero.
Otherwise, returns 0.

• int ferror ( FILE *fp );


– Checks whether the error indicator has been set for fp. (for
example, write errors to the file.)
Some other file handling
functions
• int fseek(FILE *fp, long int offset, int
origin);
– To set the current position associated with fp, to a new
position = origin+offset.
– Origin can be:
• SEEK_SET: beginning of file
• SEEK_CURR: current position of file pointer
• SEEK_END: End of file

• int ftell(FILE *fp)


– Returns the current value of the position indicator of the
stream.
Opening Files
• There are other modes for opening
files, as well.
– “r+” : open a file for read and write
(update). The file must be present.
– “w+” : write/update. Create an empty file
and open it both for input and output.
– “a+” : append/update. Repositioning
operations (fseek etc.) affect next read.
Output is always at the end of file.
An Exercise
• Often, events in a system are logged on
to a particular file. (e.g. usb drive
mounted, user logs off etc.)
• These log files can be quite large. We
are usually interested in the latest
events (maybe the last 10 events.)
• The unix command “tail <filename>”
prints the last 10 lines of <filename>.
Can you program this?
Tail – Algorithm 1
1. Write a program to pick out the first 10 lines
of a file
2. Write a function to reverse a file.
3. Reverse each line in the head of reverse of
the file.

• This algorithm is inefficient – reverse will read


the whole file. Can you think of an algorithm
which will read only the last 10 lines?
• (Hint: Start at end of file, and use fseek.)
C Switch-Case statement
• Another statement to help program
conditional blocks.
• Syntax:
switch ( expression ) {
case constant-expression: statement;
case constant-expression: statement;

case constant-expression: statement;
default: statement;
}
Switch statement execution
• First, the integer expression in switch is evaluated.

• Control passes to the statement whose case constant-


expression matches the value of the expression.

• The constants in the different cases must be unique

• Execution of the statement begins at the selected


statement, and continues until end of the switch
block, or until a break.

• default statement is executed if no case is matched.


Example
• Let us consider the code to add a suffix
to the days of a month – for example, 1
has suffix “st”, 2 has suffix “nd”, 3 “rd”,
4 “th”, and so on.

• The code can be written as follows:


char *suffix (int date)
{
char str[3];
if (date/10 == 1){
return “th”;
}
switch (date%10) {
case 1: strcpy(str, “st”);
break;
case 2: strcpy(str, “nd”);
break;
case 3: strcpy(str, “rd”);
break;
default: strcpy(str, “th”);
break;
}
return str;
}
• The code illustrates the use of break in a new context.

• Break causes the code to exit out of the innermost switch


statement. (here there’s only one.)

• If break is absent, the code will “fall-through” and execute


the next case. For example, if break is absent in the code,
and day%10 is 2, it will execute
– Strcpy(str, “nd”);
– Strcpy(str, “rd”);
– strcpy(str, “th”);

• This is usually not what we want – each case must be


separate. So end the logic of each case with a break.

• Default case: executed when no case matches. Usually placed


as the last case. Strictly speaking, break unnecessary in the
default case code, but recommended.
Acknowledgments: This lecture slide is based on the material prepared by Prof.
Sumit Ganguly, CSE, IIT Kanpur. The slide design is based on a template by Prof.
Krithika Venkataramani.

“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.”

You might also like