CPNM Lecture 17 File Handling
CPNM Lecture 17 File Handling
Jadavpur University
2023
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 1 / 24
Introduction
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 2 / 24
The FILE Structure
FILE *fp;
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 3 / 24
Text vs. Binary Mode
Text mode
Host system may perform transformations on data written to or read
from files; Ex- A new line may be converted to a
line-feed/carriage-return pair
There may not be a one to one relationship between the characters
that are written (or read) and those stored on the external device
Number of characters may not be the same as the number of
characters that is stored on the external device
Binary mode
No character translation occurs
An implementation defined number of null bytes may be appended to a
binary stream
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 4 / 24
Opening a File
fopen() function opens a file and returns the file pointer associated
with that file
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 5 / 24
File Modes I
“r”: Open an existing file for reading only
Fails if the file does not exist or the host system does not permit you to
read
“w”: Open a new file for writing only
Always creates a file, if the file exists its old contents are discarded
“a”: Open an existing file for appending only
Creates the file if it does not exist, otherwise writes new data at the
end of the existing file content
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 6 / 24
File Modes II
FILE * fp;
if((fp=fopen("test", "w"))==NULL){
printf("Cannot open file\n");
exit(0);
}
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 7 / 24
Closing a File
fclose() function
Closes a file that was opened by a call to fopen()
Writes any data still remaining in the disk buffer to the file
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 8 / 24
Character I/O
Writing a character
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 9 / 24
Using feof()
Returns true if the end of file has been reached; otherwise it returns
zero
while(!feof(fp)) ch=fgetc(fp);
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 10 / 24
Example - Keyboard to Disk
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
FILE *fp;
char ch;
if(argc!=2){
printf("You forgot to enter the filename\n");
exit(0);
}
if((fp=fopen(argv[1], "w"))==NULL){
printf("Cannot open file \n");
exit(0);
}
ch=getchar();
while(ch!='$'){
fputc(ch, fp);
ch=getchar();
}
fclose(fp);
return(0);
}
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 11 / 24
Example - Disk to Screen
#include <stdio.h>
#include <stdlib.h>
if(argc!=2){
printf("You forgot to enter the filename\n");
exit(0);
}
if((fp=fopen(argv[1], "r"))==NULL){
printf("Cannot open file \n");
exit(0);
}
ch=fgetc(fp);
while(ch!=EOF){
putchar(ch);
ch=fgetc(fp);
}
fclose(fp);
return(0);
}
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 12 / 24
Example - File Copy
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
FILE *in, *out; char ch;
if(argc!=3){
printf("You forgot to enter the filename\n");
exit(0);
}
if((in=fopen(argv[1], "r"))==NULL){
printf("Cannot open source file \n");
exit(0);
}
if((out=fopen(argv[2], "w"))==NULL){
printf("Cannot open destination file \n");
exit(0);
}
do{
ch=fgetc(in);
if(feof(in))
break;
fputc(ch, out);
}while(1);
fclose(in); fclose(out);
return(0);
}
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 13 / 24
String I/O
Reads a string from the specified file until either a newline character is
read or length-1 characters have been read
If newline is read it will be part of the string
The resulting string will be NULL terminated
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 14 / 24
The rewind() Function
Resets the file position pointer indicator to the beginning of the file
specified as its arguments
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 15 / 24
Example - File Copy
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
FILE *fp;
char str[80];
if((fp=fopen("TEST", "w"))==NULL){
printf("Cannot open file \n");
exit(0);
}
do{
printf("Enter a string (CR to quit):\n");
gets(str);
strcat(str, "\n");
fputs(str, fp);
}while(*str!='\n');
rewind(fp);
while(!feof(fp)){
fgets(str, 79, fp);
printf(str);
}
return(0);
}
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 16 / 24
Erasing a File
remove() function
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 17 / 24
Flushing
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 18 / 24
fread() and fwrite()
To read and write data types that are longer than 1 byte
buffer is pointer to a region of memory that will receive the data from
the file
count is number items read with each item being num byte bytes in
length
Returns the number of items read
size_t fwrite(const void *buffer, size_t num_bytes, size_t count, FILE *fp);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
fwrite(&d, sizeof(double), 1, fp);
struct temp{ fwrite(&i, sizeof(int), 1, fp);
char name[30]; fwrite(&l, sizeof(long), 1, fp);
int age; fwrite(str, sizeof(char), strlen(str), fp);
char sub[10]; fwrite(&t1, sizeof(struct temp), 1, fp);
}; rewind(fp);
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 20 / 24
fseek() and Random Access I/O
Sets the file position indicator numbytes distance away from origin
origin can be one of the following
SEEK SET Beginning of the file
SEEK CUR Current position
SEEK END End of file
Returns zero if successful, a nonzero value otherwise
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 21 / 24
ftell()
Returns -1 if failure
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 22 / 24
fprintf() and fscanf()
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 23 / 24
Example I
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct temp{
strcpy(str, "Ravi");
char name[30];
fprintf(fp, "%f%d%ld %s ", d, i, l, str);
int age;
fprintf(fp, "%s %d %s", t1.name, t1.age, t1.sub);
char sub[10];
fclose(fp);
};
if((fp=fopen("test", "r"))==NULL){
int main(void){
printf("Cannot open file \n");
FILE *fp;
exit(0);
float d=12.23, e;
}
int i=101, j;
fscanf(fp, "%f%d%ld%s", &e, &j, &m, s);
long l=123023, m;
fscanf(fp, "%[^$]s%c%d%s", t2.name, &c, &t2.age,
char str[10];
t2.sub);
char s[10];
printf("%f %d %ld %s", e, j, m, s);
char c;
printf("%s %d %s\n", t2.name, t2.age, t2.sub);
struct temp t1={"ABC XYZ$", 18, "BCSE"}, t2;
fclose(fp); return(0);
}
if((fp=fopen("test", "w"))==NULL){
printf("Cannot open file \n");
exit(0);
}
Mridul Sankar Barik (Jadavpur University) CPNM Lecture 17 - File Handling 2023 24 / 24