Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS115_ 2006-2007SENEM KUMOVA METIN 1 FILE OPERATIONS.

Similar presentations


Presentation on theme: "CS115_ 2006-2007SENEM KUMOVA METIN 1 FILE OPERATIONS."— Presentation transcript:

1 CS115_ 2006-2007SENEM KUMOVA METIN 1 FILE OPERATIONS

2 CS115_ 2006-2007 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)

3 CS115_ 2006-2007 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 int main() { FILE *f;// FILE * fp,x, filepointer; f = fopen("test.txt","w"); /* opens test.txt in write (w) mode */ return 0; } mode in double quotas filename

4 CS115_ 2006-2007 SENEM KUMOVA METIN 4 OPEN A FILE :MODES fp= fopen(“x.txt”, “…”) rOpen for reading r+Open for reading and writing wOpen 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. aOpen 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.

5 CS115_ 2006-2007 SENEM KUMOVA METIN 5 CLOSE A FILE : fclose() #include main() { FILE * di; if( (di=fopen(“test.txt”,”r+”))==NULL) { puts(“FILE CANNOT BE OPENED!”); exit();} ……. fclose(di); // fclose(filepointer) }

6 CS115_ 2006-2007 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 #include // 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); }

7 CS115_ 2006-2007 SENEM KUMOVA METIN 7 fputs() && fgets() fputs(string, filepointer) // puts the string to file EXAMPLE: #include #include // 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); } fgets(string,sizeof string, filepointer) // gets the string from file EXAMPLE: #include #include // 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); }

8 CS115_ 2006-2007 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);}

9 CS115_ 2006-2007 SENEM KUMOVA METIN 9fprintf 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 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);}

10 CS115_ 2006-2007 SENEM KUMOVA METIN 10fscanf() 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 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); }

11 CS115_ 2006-2007 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); }

12 CS115_ 2006-2007 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 – place can be SEEK_SET:beginning of the file SEEK_CUR :current position SEEK_END:end of the file

13 CS115_ 2006-2007 SENEM KUMOVA METIN 13 Accessing a File Randomly :EXAMPLE #include 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));} OUTPUT : 11 10 e 11 9 d 10 0 s 1

14 CS115_ 2006-2007 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


Download ppt "CS115_ 2006-2007SENEM KUMOVA METIN 1 FILE OPERATIONS."

Similar presentations


Ads by Google