CPNM Lecture 17 File Handling
CPNM Lecture 17 File Handling
Jadavpur University
2024
Introduction
FILE *fp;
▶ 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
Opening a File
FILE * fp;
if((fp=fopen("test", "w"))==NULL){
printf("Cannot open file\n");
exit(0);
}
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
▶ Writing a character
while(!feof(fp)) ch=fgetc(fp);
#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);
}
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);
}
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);
}
String I/O
#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);
}
Erasing a File
▶ remove() function
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);
▶ Returns -1 if failure
fprintf() and fscanf()
#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);
}