150 likes | 339 Views
FILE OPERATIONS. FILE OPERATIONS. Create and open the file Close the file Write a character to the file Read a character from the file Check if you are at the end of file Read a string from the file Write a string to the file Write to the file in formatted form
E N D
FILE OPERATIONS SENEM KUMOVA METIN
FILE OPERATIONS • Create and open the file • Close the file • Write a character to the file • Read a character from the file • Check if you are at the end of file • Read a string from the file • Write a string to the file • Write to the file in formatted form • Read from the file in formatted form • Write an array to the file • Read an array from the file • Reach the data from a specified place in the file (access the file randomly) SENEM KUMOVA METIN
OPEN A FILE • Each file to be opened must have a file pointer • “FILE” is the structure used to declare a file pointer (in stdio.h) • fopen() function is used to open the file. • will return a file pointer or a NULL • if it returns a NULL, then it means that file cannot be opened FILE *fopen(char *filename, char *mode) EXAMPLE : #include<stdio.h>int main(){ FILE *f; // FILE * fp,x, filepointer; f = fopen("test.txt","w"); /* opens test.txt in write (w) mode */ return 0;} filename mode in double quotas SENEM KUMOVA METIN
OPEN A FILE :MODES fp= fopen(“x.txt”, “…”) r Open for reading r+ Open for reading and writing w Open for writing and create the file if it does not exist. If the file exists then make it blank. w+ Open for reading and writing and create the file if it does not exist. If the file exists then make it blank. a Open for appending(writing at the end of file) and create the file if it does not exist. a+ Open for reading and appending and create the file if it does not exist. SENEM KUMOVA METIN
CLOSE A FILE : fclose() #include<stdio.h> main() { FILE * di; if( (di=fopen(“test.txt”,”r+”))==NULL) { puts(“FILE CANNOT BE OPENED!”); exit();} ……. fclose(di); // fclose(filepointer) } SENEM KUMOVA METIN
PUTC() && GETC() putc(character , filepointer) // puts a character to the file character=getc(filepointer) // gets a character from the file EXAMPLE : #include<stdio.h> #include<stdlib.h> // for exit() function main() { FILE * di; char kr; if( (di=fopen("test.txt", "a"))==NULL) { printf("cannot open file\n"); exit(0); } while(1) { kr=getc(stdin); if(kr!='q') { putc(kr,di); putc(kr,stdout);} else { fclose(di); exit(0); } } fclose(di); } SENEM KUMOVA METIN
fputs() && fgets() • fgets(string,sizeof string, filepointer) // gets the string from file EXAMPLE: #include<stdio.h> #include<stdlib.h> // for exit main() { FILE * di; char filename[80], string[256]; printf("Please write the file name:\n"); gets(filename); if( (di=fopen(filename, “r"))==NULL) { printf("cannot open file\n"); exit(0); } while(!feof(di)) { fgets(string, 256, di); fputs(string, stdout); // if(!feof(di)) fputs(string,stdout); } fclose(di); } • fputs(string, filepointer) // puts the string to file EXAMPLE: #include<stdio.h> #include<stdlib.h> // for exit main() { FILE * di; char filename[80], string[256]; printf("Please write the file name:\n"); gets(filename); if( (di=fopen(filename, "w"))==NULL) { printf("cannot open file\n"); exit(0); } while(1) { printf("NAME and SURNAME: "); gets(string); if(string[0]== '\0') break; else { fputs(string, di); fputs("\n",di); } } fclose(di); } SENEM KUMOVA METIN
fprintf && fscanf() • int fprintf(FILE *stream, char *format, args..) /*writes formatted data to the file */ • int fscanf(FILE *stream, char *format, args..) /*reads formatted data from the file */ EXAMPLE: main() { FILE * di; char str [100]; if( (di=fopen("tt.txt", "w"))==NULL) { printf("cannot open file\n"); exit(0); } fprintf(di,"%s world \n","hello"); fclose(di); if( (di=fopen("tt.txt", "r"))==NULL) { printf("cannot open file\n"); exit(0); } while(!feof(di)) { fscanf(di,"%s", str); printf("%s", str); } fclose(di); } SENEM KUMOVA METIN
fprintf • int fprintf(FILE *stream, char *format, args..) /*writes formatted data to the file */ • int fscanf(FILE *stream, char *format, args..) /*reads formatted data from the file */ EXAMPLE: CALCULATE THE RADIAN and COSINUS #include<stdio.h> #include<math.h> main() { FILE * di; int angle; float radian, h; if( (di=fopen(“cosinus.aci”, "w"))==NULL) { printf("cannot open file\n"); exit(0); } for (angle=0; angle<=360; angle++) { radian=(float) angle*3.14/180; h=cos(radian); fprintf(di,”%d, %f, %f\n”,angle,radian,h); } fclose(di); } SENEM KUMOVA METIN
fscanf() • int fprintf(FILE *stream, char *format, args..) /*writes formatted data to the file */ • int fscanf(FILE *stream, char *format, args..) /*reads formatted data from the file */ EXAMPLE: READ cosinus.aci FILE #include<stdio.h> #include<stdlib.h> main() { FILE * di; int angle; float radian, h; char str [100]; if( (di=fopen(“cosinus.aci”, “r"))==NULL) { printf("cannot open file\n"); exit(0); } while(!feof(di)) { fscanf(di,”%d %f %f”, &angle, &radian, &h); printf(”%d, %f, %f\n”, angle, radian, h); } fclose(di); } SENEM KUMOVA METIN
fread() && fwrite() // fread(array_name,sizeof array element,total data size,filepointer) // fwrite (array_name,sizeof array element,total data size,filepointer) int main() { FILE *f; int buf1[5]={'a', 'b', 'c', 'd', 'e'}; int buf2[3]; if(( f = fopen("test.txt","w")) ==NULL) exit(0); fwrite(buf1,sizeof(buf1[0]),5,f); fwrite(buf1,sizeof(buf1[0]),5,stdout); fclose(f); printf("\n"); if(( f = fopen("test.txt","r")) ==NULL) exit(0); fread(buf2,sizeof(buf2[0]),3,f); fwrite(buf2,sizeof(buf2[0]),3,stdout); fclose(f); } SENEM KUMOVA METIN
Accessing a File Randomly • ftell(file_pointer) • returns the current value of file position indicator • returned value represents the number of bytes from the beginning of the file • fseek(file_pointer,offset, place) • sets the file position indicator to a value that represents offset bytes from place • placecan be • SEEK_SET :beginning of the file • SEEK_CUR :current position • SEEK_END :end of the file SENEM KUMOVA METIN
Accessing a File Randomly :EXAMPLE OUTPUT : 11 10 e 11 9 d 10 0 s 1 #include<stdio.h> main() { char name[100] ="sample code"; FILE * fp; fp= fopen("text.tx","w+"); fprintf(fp,"%s", &name[0]); printf(“%d\n", ftell(fp)); fseek(fp,-1,SEEK_CUR); printf(“%d\n", ftell(fp)); printf("%c\n",getc(fp)); printf("%d\n", ftell(fp)); fseek(fp,-2,SEEK_CUR); printf("%d\n", ftell(fp)); printf("%c\n",getc(fp)); printf("%d\n", ftell(fp)); fseek(fp,0,SEEK_SET); printf("%d\n", ftell(fp)); printf("%c\n",getc(fp)); printf("%d\n", ftell(fp)); } SENEM KUMOVA METIN
FILE OPERATIONS fopen() : create and open files fclose() : closes files putc() : writes a character to file getc() : reads a character from file feof() : checks if pointer has arrived to the end of file fgets() : reads a string from file fputs() : writes a string to file fprintf() : writes to file in a formatted form fscanf() : reads from file in a formatted form fwrite() : writes an array to a file fread() : reads an array from a file fseek() : reaches the data from a specified place in file SENEM KUMOVA METIN