1 / 14

FILE OPERATIONS

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

Download Presentation

FILE OPERATIONS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. FILE OPERATIONS SENEM KUMOVA METIN

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

More Related