Files 1 יו×‌ ×—×‍ישי 16 ×פריל 2015 יו×‌ ×—×‍ישי 16 ×פריל 2015 יו×‌ ×—×‍ישי

  • View
    222

  • Download
    5

Embed Size (px)

Text of Files 1 יו×‌...

  • Slide 1

Files 1 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 Department of Computer Science-BGU Slide 2 Open File FILE fopen(const char filename, const char mode) : filename : ( ). mode : . : "r" - . "w" - . "a" - , . 2 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 Department of Computer Science-BGU Slide 3 Open File (cont.) fopen() , NULL. : 1) , , . 2) , (overwrite). 3) , NULL. fopen() . FILE . NULL . 3 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 Department of Computer Science-BGU Slide 4 Example , "in.dat" : FILE fin ; fin=fopen( in.dat , r ); if( fin==NULL) { printf( Error in opening file %s\n , in.dat ) ; exit(1) ; } : 1) , fin . 2) - fopen() "r", . 3) fopen() , exit(). 1 . 4 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 Department of Computer Science-BGU Slide 5 . , . fclose() - stdio.h : int fclose( FILE ) 5 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 Department of Computer Science-BGU Slide 6 Character in file Get a character int fgetc( FILE *stream ); Each of these functions returns the character read. To indicate a read error or end-of-file condition, fgetc and getchar return EOF. Put a character int fputc( int c, FILE *stream ); 6 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 Department of Computer Science-BGU Slide 7 Example Write program that uses fgetc and fputc to read words from the input file and place them into the output file with one space a separator. The maximal word size is 80 characters. 7 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 Department of Computer Science-BGU Slide 8 Solution #include #define MAX 80 void main (){ FILE *source, *result; char string [MAX+1]; int ch, i=0; if((source=fopen("source.txt","r"))== NULL{ printf("Can't open the file\n"); exit(1); } if((result= fopen("result.txt","w"))==NULL){ printf("Can't open the file\n"); exit(1); } while((ch = fgetc(source))!=EOF) if( ch!=' ') string[i++]=ch; else if(i) { string[i]='\0'; for(i=0; string[i]; i++) fputc(string[i],result); fputc(' ',result); i=0; } if(i) { string[i]='\0'; for(i=0; string[i]; i++) fputc(string[i], result); } fclose(source); fclose(result); } 8 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 Department of Computer Science-BGU Slide 9 String in file Get a string char *fgets( char *string, int n, FILE *stream ); Put a string int fputs( char *string, FILE *stream ); 9 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 Department of Computer Science-BGU Slide 10 Properties of fgets The fgets function reads characters from the current stream position to and including the first newline character, to the end of stream, or until the number of characters read is equal to n-1, whichever comes first. The result stored in string is appended with a NULL character. The newline character, if read, is included in the string. 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 Department of Computer Science-BGU 10 Slide 11 Example Write program that uses fgets to read lines from the input file. Then the program uses fputs to put words into the output file retaining one space between the words. The maximal line size is 80 characters. 11 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 Department of Computer Science-BGU Slide 12 Solution #include #define MAX 80 void main(){ FILE *source, *result; char string [MAX+1]; char *save; int i=0, mode=1; if((source= fopen("source.txt","r"))==NULL){ printf("Can't open the file\n"); exit(1); } if((result= fopen("result.txt","w"))==NULL) { printf("Can't open the file\n"); exit(1); } while(fgets(string,MAX+1,source)) { save=string; while(*(save)){ if(mode) { if(*(save)==' ') mode=0; save++; } else if(*(save)!=' ') { mode=1; save++; } else strcpy(save,save+1); fputs(string,result); } fclose(source); fclose(result); } 12 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 Department of Computer Science-BGU Slide 13 Formatted Input/Output int fscanf( FILE *stream, const char *format [, argument ]... ); int fprintf( FILE *stream, const char *format [, argument ]...); 13 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 Department of Computer Science-BGU Slide 14 Example Write program that uses fscanf and fprintf to read words from the input file and places them into the output file with one space a separator. The maximal word size is 80 characters. 14 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 Department of Computer Science-BGU Slide 15 Solution #include #define MAX 80 void main(){ FILE *source, *result; char string [MAX+1]; if(((source=fopen("source.txt","r"))==NULL) || ((result=fopen("result.txt","w"))==NULL)) { printf("Can't open the file\n"); exit(1); } while((fscanf(source,"%s", string))!=EOF) fprintf(result,"%s ",string); fclose(source); fclose(result); } 15 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 Department of Computer Science-BGU Slide 16 fscanf - format specification Fields A format specification has the following form: %[*] [width] type If the first character in the set is a caret (^), the effect is reversed: The input field is read up to the first character that does appear in the rest of the character set. For example: fscanf( ptr, "%20[^#]%9d%*c", name, &id); the function fscanf reads 20 characters, or till letter (#'), or till newline from the input stream and stores them in field name, then it reads the next 9 characters and converts them into integer id, then it reads one symbol which is not stored. 16 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 Department of Computer Science-BGU Slide 17 Example 1 , : ( 20 ) . . (9 ) (4 ) (3 ) (4 ) (3 ).....( ) . . . 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015 16 2015