File Input / Output File open FILE *fopen( filename, mode ); The fopen command can open an external file in C language. If the file is exist, the file pointer is not NULL. Otherwise, the file pointer is NULL. { FILE *fp; if ( (fp=fopen("test.txt","r")) == NULL) printf("\nopen file error!"); else printf("\nopen file success!"); Lecture 9, C Language,Page 1/13
File open mode r Open a text file for reading w Open a text file for writing, if the file does not exist then create a new one a Append to the text file r+ Read and Write w+ Read and Write, if the file does not exist then create a new one a+ Append and Write rb Open a binary file for reading wb Open a binary file for writing ab Append to a binary file Text file Reading and Closing After you finish processing the file, you need to close it. int fclose(file *); /* success return 0, error return 1 */ Get a character form file char fgetc(file *); Lecture 9, C Language,Page 2/13
#include <stdlib.h> { FILE *fp; char ch; if ( (fp=fopen("test.txt","r")) == NULL) { printf("\nopen file error!"); exit(1); else printf("\nopen file success!\n"); while ( (ch=fgetc(fp))!= EOF ) /* Read until EOF */ printf("%c",ch); fclose(fp); Output open file success! line 1 line 2 line 3 Lecture 9, C Language,Page 3/13
Text file Reading, Writing and Closing #include <stdlib.h> { FILE *fp1,*fp2; /* fp1 source file */ char ch; /* fp2 target file */ /* open file for reading */ if ( (fp1=fopen("test.txt","r")) == NULL) { printf("\nopen file error!"); exit(1); else printf("\nopen file success!\n"); fp2=fopen("copy.txt","w"); /* open file for writing */ while ( (ch=getc(fp1))!= EOF ) { putchar(ch); /* Output to screen */ fputc(ch,fp2); /* Output to file */ fclose(fp1); fclose(fp2); Lecture 9, C Language,Page 4/13
fprintf for file writing printf(file *, %d.%f.., var1, var2); #include <stdlib.h> { FILE *fp1; /* fp1 source file */ char ch; /* fp2 target file */ fp1=fopen("test.txt","w"); /* open file for writing */ fprintf(fp1,"this is message %d\n",1); fprintf(fp1,"this is message %d\n",2); fclose(fp1); Output This is message 1 This is message 2 fscanf for file reading End of file fscanf(..%d..%f,&var1,&var2); int feof(file *) Lecture 9, C Language,Page 5/13
#include <stdlib.h> { FILE *fp1; /* fp1 source file */ char ch; /* fp2 target file */ char ivalue[50]; fp1=fopen("test.txt","r"); /* open file for reading */ while (!feof(fp1)) { fscanf(fp1,"%s",ivalue); printf("\n%s",ivalue); fclose(fp1); Input This is message 1 This is message 2 Output This is message 1 This is message 2 Lecture 9, C Language,Page 6/13
Exercise 9.1 Write a program that can encrypt and decrypt a document. Use command line input e for encryption and d for decryption. Ex1 e source.txt enc.txt Ex1 d enc.txt dec.txt Source.txt this is message 1 this is message 2 enc.txt Encryption function unsigned char encrypt(unsigned char ch) { ch=ch<<1; return ch; Decryption function unsigned char decrypt(unsigned char ch) { ch=ch>>1; return ch; Lecture 9, C Language,Page 7/13
Output: C:\My Documents\notes\C\lecture9>ex1 e source.txt enc.txt open file success! C:\My Documents\notes\C\lecture9>ex1 d enc.txt dec.txt open file success! this is message 1 this is message 2 C:\My Documents\notes\C\lecture9> Binary file writing int fwrite(data pointer, size of data, count, FILE *); #include <conio.h> struct score { char name[20]; int marks; ; { FILE *fp; /* fp1 source file */ score var; fp=fopen("test.bin","wb"); /* open file for writing */ do { printf("\nplease enter name :"); fflush(stdin); Lecture 9, C Language,Page 8/13
gets(var.name); printf("please enter marks :"); fflush(stdin); scanf("%d",&var.marks); fwrite(&var,sizeof(var),1,fp); printf("do you wish to continue?"); while (getche()=='y'); fclose(fp); Output Please enter name :name 1 Please enter marks :10 Do you wish to continue?y Please enter name :name 2 Please enter marks :20 Do you wish to continue?y Please enter name :name 3 Please enter marks :30 Do you wish to continue?y Please enter name :name 4 Please enter marks :40 Do you wish to continue?n Lecture 9, C Language,Page 9/13
Binary file reading int fread(data pointer, size of data, count, FILE *); #include <conio.h> struct score { char name[20]; int marks; ; { FILE *fp; /* fp1 source file */ score var; fp=fopen("test.bin","rb"); /* open file for reading */ while (fread(&var,sizeof(var),1,fp)!= 0) { printf("name : %s\n",var.name); printf("marks : %d\n",var.marks); fclose(fp); Output Name : name 1 marks : 10 Name : name 2 marks : 20 Name : name 3 marks : 30 Name : name 4 marks : 40 Lecture 9, C Language,Page 10/13
fseek int fseek(file *stream, long offset, int origin); origin 0 From the beginning of the file 1 From the current position 2 From the end of the file #include <conio.h> struct score { char name[20]; int marks; ; { FILE *fp; /* fp1 source file */ score var; fp=fopen("test.bin","rb"); /* open file for writing */ if (fseek(fp,sizeof(var)*2,2)!=0) { printf("random access file pointer error"); if (fread(&var,sizeof(var),1,fp)!= 0) { printf("name : %s\n",var.name); printf("marks : %d\n",var.marks); else printf("file reading error"); fclose(fp); Lecture 9, C Language,Page 11/13
Return the current position int ftell(file *); Reset the position to zero void rewind(file *); #include <conio.h> struct score { char name[20]; int marks; ; { FILE *fp; /* fp file pointer */ score var; fp=fopen("test.bin","rb"); /* open file for writing */ clrscr(); printf("\nfp=%d",ftell(fp)); /* return current position */ if (fseek(fp,sizeof(var)*2,1)!=0) /* go to somewhere */ printf("random access file pointer error"); printf("\nfp=%d",ftell(fp)); /* return current position */ if (fread(&var,sizeof(var),1,fp)!= 0) { printf("\nname : %s\n",var.name); printf("marks : %d\n",var.marks); else printf("file reading error"); printf("\nfp=%d",ftell(fp)); /* return current position */ Lecture 9, C Language,Page 12/13
rewind(fp); /* reset to position zero */ printf("\nfp=%d",ftell(fp)); /* return current position */ fclose(fp); Output : FP=0 FP=44 Name : test3 marks : 30 FP=66 FP=0 Lecture 9, C Language,Page 13/13