Module 13 - part 2 - file handling
Module 13 - part 2 - file handling
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
File
• Applications require information to be read from or written to
memory device which can be accomplished using files.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
File handling Basics
• A file has to opened before data can be read from or written to it.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
fopen: Opening a File
• FILE *fp; Defines file pointer
• fp = fopen(“foo.txt”, “r”); Only reading permitted
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
File Opening Modes
• r – Reading a file
• w – Writing a file
• a – Appending to the end of an existing file
When used with “w” or “a”, fopen creates a file if it does not
find one.
fopen will fail if the file does not have the necessary permissions
(r, w and a) or if the file does not exist in case of “r”.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
File Opening Modes: Extended
• Database applications often need both read and write access
for the same file, in which case, you must consider using the
“r+”, “w+” and “a+” modes.
Mode File opened for
r Reading only
r+ Both reading and writing
w Writing only
w+ Both reading and writing
a Appending only
a+ Reading entire file but only appending permitted
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
File Read/Write Functions
The standard library offers a number of functions for performing
read/write operations on files.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
fclose: Closing a file
• Operating systems have a limit on the number of files that can
be opened by a program.
• fclose(fp);
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example 1
int main(){
FILE *fp; char buf1[80], buf2[80];
fputs("Enter a line of text: \n", stdout);
fgets(buf1, 80, stdin);
fp = fopen("foo", "w"); if (fp == NULL){
fputs(buf1, fp); fputs(“Error”, stdout);
return 0;
fclose(fp); }
fp = fopen("foo", "r");
fgets(buf2, 80, fp);
fputs(buf2, stdout);
fclose(fp);
return 0;
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
OUTPUT
fputs(buf1, fp);
fputs(buf2, stdout);
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
OUTPUT
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Practice problems
• Write a C program which prints itself! [Hint: use file handling]
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example 1:
int main(int argc, char *argv[]){
printf(“Number of arguments: %d \n", argc);
int i = 0;
while(i < argc){
printf("Argument %d: %s \n",i, argv[i]);
i++;
} gcc test.c
./a.out one two three
return 0;
Number of arguments: 4
}
Argument 0: ./a.out
Argument 1: one
Argument 2: two
Argument 3: three
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example 2: To display the
content of one file
#include <stdio.h>
int main(int argc, char *argv[]){
FILE *fp; char ch;
fp = fopen(argv[1], "r");
if (fp == NULL){
printf("Error");
return(0);
}
ch = fgetc(fp);
while (ch != EOF){
printf("%c", ch);
ch = fgetc(fp);
}
fclose(fp);
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example 3: To display the
content of more than one file
#include <stdio.h>
int main(int argc, char *argv[]){
int nof = argc-1;
while (nof > 0){
FILE *fp; char ch;
fp = fopen(argv[nof],"r");
ch = fgetc(fp);
while (ch != EOF){
printf ("%c", ch);
ch = fgetc(fp);}
nof--;
fclose (argv[nof]);}
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus