1 / 31

Files

Files. Open File. FILE  fopen (const char  filename , const char  mode) כאשר : filename : הינו מצביע למחרוזת תווים שהינה שם הקובץ אותו רוצים לפתוח ( יש לציין מסלול מלא ביחס למיקום התוכנית) . mode : הינו מצביע למחרוזת תווים אשר מציינת את מטרת פתיחת הקובץ . האופציות הן :

heatherq
Télécharger la présentation

Files

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. Files Department of Computer Science-BGU

  2. Open File FILEfopen(const char filename , const char mode) כאשר : filename : הינו מצביע למחרוזת תווים שהינה שם הקובץ אותו רוצים לפתוח ( יש לציין מסלול מלא ביחס למיקום התוכנית) . • mode : הינו מצביע למחרוזת תווים אשר מציינת את מטרת פתיחת הקובץ . האופציות הן : "r" - פתיחת קובץ טקסט לקריאה. "w" - פתיחת קובץ טקסט לכתיבה. "a" - פתיחת קובץ טקסט להוספה, בסופו של הקובץ. Department of Computer Science-BGU

  3. Open File (cont.) • במידה והפונקציה fopen() הצליחה בפתיחת הקובץ יוחזר מצביע לקובץ, אחרת יוחזר NULL . • מספר נקודות : • 1)במידה ונפתח לכתיבה או הוספה, קובץ שלא קיים, יווצר קובץ בשם זה. • 2)במידה ונפתח לכתיבה קובץ שקיים, הכתיבה לקובץ זה תמחק את הקיים (overwrite). • 3)במידה ונפתח לקריאה קובץ שלא קיים או ללא הרשאה מתאימה, הפונקציה תחזיר NULL. • כפי שניתן לראות הפונקציה fopen() מחזירה מצביע לקובץ שאיתו אנו רוצים לעבוד. בכדי לשמור כתובת זו נגדיר משתנה מטיפוס FILE . • אם הפונקציה תחזיר למשתנה זה NULL סימן שהפתיחה לא הצליחה ולכן אין טעם להמשיך במהלך ביצוע התוכנית. Department of Computer Science-BGU

  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 כמציין סיום לא נורמלי של התוכנית. Department of Computer Science-BGU

  5. סגירת קובץ • בסיום השימוש בקובץ או עם סיום התוכנית חייבים לסגור את הקבצים שפתחנו. אם לא נעשה זאת, חלק מהמידע שכתבנו לקובץ עלול להיאבד. לצורך כך קיימת פונקצית הספרייה fclose() אשר אב הטיפוס שלה מוגדר גם כן ב - stdio.h והוא : int fclose( FILE ) Department of Computer Science-BGU

  6. Character in file Get a character intfgetc( 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 intfputc( int c, FILE *stream ); Department of Computer Science-BGU

  7. Example 1 Write program that uses fgetc and fputc to copy from the input file “source.txt” to the output file result.txt”. Department of Computer Science-BGU

  8. Solution 1  #include <stdio.h> #include <stdlib.h> void main (){ FILE *source, *result; char string [MAX+1]; int ch, i=0; if((source=fopen("source.txt","r"))==NULL || (result= fopen("result.txt","w"))==NULL){ printf("Can't open the files\n"); exit(1); } while((ch = fgetc(source))!=EOF) fputc(string[i], result); fclose(source); fclose(result); } Department of Computer Science-BGU

  9. 2Example 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. Department of Computer Science-BGU

  10. Solution 2  #include <stdio.h> #include <string.h> #include <stdlib.h> #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); } Department of Computer Science-BGU

  11. Results 2 “source.txt” ahd aldja jdod qjjdjkj kkjl;;D JDJ JKJKJ k kkklljl hhhhfa;; jj kk “result.txt” ahd aldja jdod qjjdjkj kkjl;;D JDJ JKJKJ k kkklljl hhhhfa;; jj kk Department of Computer Science-BGU

  12. String in file Get a string char *fgets( char *string, int n, FILE *stream ); Put a string int fputs( char *string, FILE *stream ); Department of Computer Science-BGU

  13. Properties of fgets The fgets function reads characters from the current stream position to (whichever comes first): • And including the first newline character. • The end of stream. • Until the number of characters read is equal to n-1 . The result stored in string is appended with a NULL character. The newline character, if read, is included in the string. Department of Computer Science-BGU

  14. 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. Department of Computer Science-BGU

  15. Solution 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); } #include <stdio.h> #include <string.h> #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); } Department of Computer Science-BGU

  16. Results “source.txt” ahd aldja jdod qjjdjkj kkjl;;D JDJ JKJKJ k kkklljl hhhhfa;; jj kk “result.txt” ahd aldja jdod qjjdjkj kkjl;;D JDJ JKJKJ k kkklljl hhhhfa;; jj kk Department of Computer Science-BGU

  17. Formatted Input/Output • int fscanf( FILE *stream, const char *format [, argument ]... ); • int fprintf( FILE *stream, const char *format [, argument ]...); Department of Computer Science-BGU

  18. 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. Department of Computer Science-BGU

  19. Solution #include <stdio.h> #include <stdlib.h> void main(){ FILE *source, *result; char string [81]; 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); } Department of Computer Science-BGU

  20. Problems using fscanf() • When we use this function : fscanf(“%20s%9d”, name, &id); • We intend to read from the file 20 letters for a first and second names , followed by 9 digits for a integer number. • The problem is that fscanf() stops on the first whitespace (blank, newline and tab). • We are not sure how many letters were red from the file ( 20 ??). Department of Computer Science-BGU

  21. 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. Department of Computer Science-BGU

  22. בשאלה הזאת מנהלים חשבונות בנק ומחשבים ריביות יומיות עבור חשבונות שביתרה שלילית. הריבית היומית עבור יתרה שלילית היא 0.03%.  נתונים שני קבצים:  קובץ הלקוחות של בנק מסוים, כל שורה בקובץ מכילה את הפרטים הבאים: שם הלקוח - 20 תווים כתובת - 20 תווים מספר ת.ז. - 9 ספרות מספר חשבון - 6 ספרות יתרת החשבון - מספר ממשי בגודל 9 (כולל 2 ספרות אחרי הנקודה)  קובץ שמכיל רשימת הפעולות בחשבונות ביממה האחרונה, כל שורה בקובץ מכילה את הפרטים הבאים: מספר חשבון - 6 ספרות סכום הפעולה - מספר ממשי בגודל 9 (כולל 2 ספרות אחרי הנקודה) סוג פעולה - סוג פעולה (תו אחד: '+' הפקדה , '-' משיכה)  יתכן שמופיעות יותר מפעולה אחת לאותו חשבון.  כתוב פונקציה לעדכון יומי void update(FILE* clients, FILE* movements, char *updateFile, char *interest)שמקבלת clients מצביע לקובץ הלקוחות, movements מצביע לקובץ התנועות, ויוצרת שני קבצים חדשים:  קובץ ששמו במחרוזת updateFileוהוא העדכון היומי של הקובץ clients בהתאםלתנועות שבקובץmovements. מבנה הקובץ זהה למבנה הקובץ clients. קובץ ששמו במחרוזת interest שמכיל סכומי ריבית על יתרה מעודכנת שלילית7 , כל שורה מכילה את הפרטים הבאים: מספר חשבון - 6 ספרות יתרת החשבון - 9 סימנים (שלילית) ריבית - 6 סימנים.  כל הקבצים מסודרים לפי מספר חשבון בסדר עולה. Department of Computer Science-BGU

  23. void update(FILE* clients, FILE* movements, char *updateFile, char *interest){ FILE *updatef, *inter; char client_det[DETAILS+6+1], account[6+1], sign, eof; double client_balance, move_balance; if(!(updatef=fopen(updateFile,"w"))) exit(1); if(!(inter=fopen(interest,"w"))) exit(1); eof=fscanf(movements,"%6s%9lf%c%*c", account, &move_balance, &sign); while(fscanf(clients,"%55[^\n]%9lf%*c",client_det,&client_balance)!=EOF){ while(eof!=EOF && !strcmp(client_det+DETAILS,account)){ if(sign=='+') client_balance+=move_balance; else client_balance-=move_balance; eof=fscanf(movements,"%6s%9lf%c%*c",account,&move_balance,&sign); } if(client_balance<0) fprintf(inter,"%6s%9.2f%6.2f\n",client_det+DETAILS,client_balance, client_balance*0.0003); fprintf(updatef,"%15s%9.2f\n",client_det,client_balance); } fclose(inter); fclose(updatef); } Department of Computer Science-BGU

  24. האוניברסיטה החליטה לפתח תכנה שתאפשר לסטודנט כל סמסטר לבחור בקלות קורסי בחירה.  מוגדר קובץ של כל הקורסים הניתנים באוניברסיטה, כל שורה בקובץ מכילה את הפרטים הבאים: שם הקורס – 20 תווים מס. הקורס – 8 ספרות קוד מחלקה – 3 ספרות ‏‏יום בשבוע – ספרה אחת (1 יום א', 2 יום ב'....) שעת התחלה – 2 ספרות שעת סיום – 2 ספרות תאריך עדכון הפרטים – 8 ספרות הקובץ ממוין לפי מס' קורס בסדר עולה. (ידוע שקורס ניתן רק פעם בשבוע)  עבור כל מסלול בכל מחלקה מוגדר קובץ המכיל פרטי קורסי בחירה שסטודנט במסלול רשאי לקחת, כל שורה בקובץ מכילה את הפרטים הבאים: מס. הקורס – 8 ספרות נקודות זיכוי – 2 ספרות תאריך עדכון הפרטים – 8 ספרות הקובץ ממוין לפי מס' קורס בסדר עולה. Department of Computer Science-BGU

  25. עבור כל סטודנט מוגדר מערך דו מימדי arrשל מערכת השעות שלו באותו סמסטר, מס' שורה (0-23) מגדיר שעה במערכת ומספר עמודה היום (0 שבת, 1 יום א', 2 יום ב'...) לדוגמה האיבר arr[14][5]מיצג את השעה 14:00 (עד 15:00) שביום ה'. שעה תפוסה במערכת סומנת ע"י 1 ושעה פנויה ע"י 0.  כתוב פונקציה void schedule(FILE* all, FILE* option, int arr[ ][7], char* filename) שמקבלת allו-optionשני מצבעים לקבצים הנ"ל , arrמערך של מערכת הסטודנט הפונקציה בונה קובץ חדש בשם filenameובו רשימת קורסי בחירה שהסטודנט יכול לקחת ללא התנגשות עם המערכת שלו, כל שורה בקובץ מכילה את הפרטים הבאים: שם הקורס – 20 תווים מס. הקורס – 8 ספרות קוד מחלקה – 3 ספרות נקודות זיכוי – 2 ספרות ‏‏יום בשבוע – ספרה אחת (1 יום א', 2 יום ב'....) שעת התחלה – 2 ספרות שעת סיום – 2 ספרות הקובץ ממוין לפי מס' קורס בסדר עולה. Department of Computer Science-BGU

  26. void schedule(FILE *all, FILE *option,intarr[][7],char *filename){ FILE *bhira; char course_all[29], course[9], depart[4], points[3]; int day, start, finish, i, eof_option; if(!(bhira=fopen(filename,"w"))) exit(1); fscanf(all,"%28[^$]%3[^$]%1d%2d%2d%9*c", course_all, depart, &day, &start, &finish); eof_option=fscanf(option,"%8[^$]%2[^$]%9*c", course, &points); while(eof_option!=EOF){ // for each optional if(!strcmp(course_all + 20, course)){//overlapping checking for(i=start; i<finish && !arr[i][day]; i++); if(i==finish){ // time array correction for(i=start; i<finish; i++) arr[i][day]=1; fprintf(bhira,"%28s%3s%2s%1d%02d%02d\n", course_all, depart, points, day, start, finish); } eof_option=fscanf(option,"%8[^$]%2[^$]%9*c", course, &points); }// if fscanf(all,"%28[^$]%3[^$]%1d%2d%2d%9*c", course_all,depart, &day, &start, &finish); }// while } Department of Computer Science-BGU

  27. נתונים שני קבצים של ציונים של סטודנטים ממחלקה מסוימת: • הקובץ הראשון מכיל פרטי ציוני המבחן. כל שורה שמכילה את הפרטים הבאים: • שם הסטודנט – עד 20 תווים • מספר ת.ז. – 9 ספרות • ציון מבחן – 3 ספורות • הקובץ השני מכיל פרטי ציוני העבודות. כל שורה שמכילה את הפרטים הבאים: • מספר ת.ז. – 9 ספרות • ציון עבודה 1 – 3 ספורות • ציון עבודה 2 – 3 ספורות • ציון עבודה 3 – 3 ספורות • שני קבצים הנ"ל ממוינים לפי מספר ת.ז בסדר עולה. • כתוב פונקציה final( . . )שמקבלת כארגומנטים שני פוינטרים על הקבצים הנ"ל ומייצרת קובץ שלישי. הקובץ השלישי מכיל ריכוז הציונים והציון הסופי של כל סטודנט. כל שורה בקובץ מכילה את הפרטים הבאים: • שם הסטודנט – עד 20 תווים • מספר ת.ז. – 9 ספרות • ציון עבודה 1 – 3 ספורות • ציון עבודה 2 – 3 ספורות • ציון עבודה 3 – 3 ספורות • ציון מבחן – 3 ספורות • ציון סופי - 3 ספרות (לפי 30% עבודות 70% בחינה) • שימו לב: אם סטודנט לא ניגש לבחינה לא יופיעו 2 הציונים האחרונים, אבל אם נבחן ולא הגיש עבודות ציוני העבודות הם אפס. • הפונקציה מקבלת את השם של הקובץ השלישי כארגומנט שלישי. Department of Computer Science-BGU

  28. void final(FILE* exam, FILE* ass, char* summery_filename) { FILE *summery; char name_ex[21],name_ass[21]; long id_exam, id_ass; int flag1, flag2, f1, f2; int exam_gr, final_gr; int gr1, gr2, gr3; summery = fopen(summery_filename,"w"); flag1 = fscanf(exam, "%[^0-9]%9d%3d%*c", name_ex, &id_exam, &exam_gr);flag2 = fscanf(ass,"%[^09]%9d%3d%3d%3d%*c",name_ass,&id_ass,&gr1,&gr2,&gr3); Department of Computer Science-BGU

  29. while (flag1 != EOF || flag2 != EOF) { f1 = (flag1 != EOF); f2 = (flag2 != EOF); if (f1 && f2 && id_exam == id_ass) { final_gr = (7*exam_gr+gr1+gr2+gr3)/10; fprintf(summery, "%s%9d%3d%3d%3d%3d%3d\n", name_ex, id_exam, gr1, gr2, gr3, exam_gr, final_gr); flag1 = fscanf(exam,"[^0-9]%9d%3d%*c", name_ex, &id_exam, &exam_gr); flag2 = fscanf(ass, "[^0-9]%9d%3d%3d%3d%*c", name_ass, &id_ass, &gr1, &gr2, &gr3); } else if ((f1 && f2 && id_exam < id_ass) || !f2) { final_gr = 7*exam_gr/10; fprintf(summery, "%s%9d%3d%3d%3d%3d%3d\n", name_ex, id_exam, 0, 0, 0, exam_gr, final_gr); flag1 = fscanf(exam, "%20s%9d%3d%*c", name_ex, &id_exam, &exam_gr); } else if ((f1 && f2 && id_exam > id_ass) || !f1) { fprintf(summery,"%s%9d%3d%3d%3d\n",name_ass,id_ass,gr1,gr2,gr3); flag2 = fscanf(ass, "%[^0-9]%9d%3d%3d%3d%*c", name_ass, &id_ass, &gr1, &gr2, &gr3); } } fclose(summery); } Department of Computer Science-BGU

  30. נתונים שני קבצים: • קובץ נתוני הסטודנטים של הקורס שלנו, כל שורה בקובץ מכילה את הפרטים הבאים: • שם הסטודנט - 20 תווים • כתובת - 20 תווים • מספר ת.ז. - 9 ספרות • קוד מחלקה - 3 ספרות • ציון סופי בקורס - 3 ספרות • הקובץ מסודר לפי מספר ת.ז. בסדר עולה. • 2) קובץ פרטי כל המחלקות של האוניברסיטה, כל שורה בקובץ מכילה את הפרטים הבאים: • - שם המחלקה - 10 תווים • - קוד מחלקה - 3 ספרות • - שם ראש המחלקה - 20 תווים • הקובץ מסודר לפי קוד מחלקה בסדר עולה. • האוניברסיטה החליטה לפרסם שמות הסטודנטים המצטיינים בקורס שלנו. סטודנט מצטיין הוא סטודנט • שקיבל את הציון הגבוה ביותר במחלקה שלו. באותה מחלקה יכולים להיות כמה סטודנטים מצטיינים. • כתוב פונקציה excellent שמקבלת כפרמטרים שני מצביעים לקבצים הנ"ל ויוצרת קובץ חדש • בשם "excellent.dat” של כל הסטודנטים המצטיינים, כל שורה בקובץ מכילה: • שם המחלקה - 10 ספרות • קוד המחלקה - 3 ספרות • שם הסטודנט - 20 תווים • מספר ת.ז. - 9 ספרות • הקובץ מסודר לפי קוד מחלקה בסדר עולה. • הגבלה: אין להעתיק קובץ למבנה נתונים אחר (מערך, רשימה משורשרת, ...) Department of Computer Science-BGU

  31. void excellent(FILE* student, FILE* department){ FILE* excel; char name[21],id[10], dp_name[11]; int cod_s, cod_d, grade, max; rewind(department); if((excel=fopen("excellent.dat","w"))== NULL){ printf("Error in access to file department.dat\n"); exit(1); } while(fscanf(department,"%10[^$]%3d%*21c",dp_name,&cod_d)==2){ max = -1; rewind(student); while(fscanf(student,"%*49c%3d%3d%*c",&cod_s,&grade)==2) if(cod_s==cod_d && grade>max) max = grade; if(max == -1)continue; rewind(student); while(fscanf(student,"%20[^$]%*20c%9s%3d%3d%*c", name,id, &cod_s, &grade)==4) if(cod_s==cod_d && grade==max) fprintf(excel,"%10s%3d%20s%9s\n",dp_name,cod_d,name,id); } (fclose(excel)); } Department of Computer Science-BGU

More Related