קבצים קרן כליף. © Keren Kalif 2 ביחידה זו נלמד:  מוטיבציה לעבודה עם קבצים  סוגי קבצים:  בינאריים  טקסט  פעולות על קבצים (עבור שני סוגי הקבצים): 

  • View
    243

  • Download
    10

Embed Size (px)

Text of קבצים קרן כליף. © Keren Kalif 2 ביחידה זו נלמד:  מוטיבציה...

  • Slide 1

Slide 2 Keren Kalif 2 : : ( ): : fseek, ftell, rewind, ferror, fflush, feof Slide 3 Keren Kalif 3 - , , - RAM (Hard Disk) : " : DB ( ) Slide 4 Keren Kalif 4 - , : " , Slide 5 Keren Kalif 5 : : Slide 6 Keren Kalif 6 *FILE - stdio.h / 1. void main() 2. { 3. FILE* f = fopen(, ); 4. if (f == NULL) 5. { 6. printf("Failed opening the file. Exiting!\n"); 7. return; 8. } 9. // here some operation with the file.. 10. fclose(f); 11. } Slide 7 Keren Kalif 7 FILE* fopen(char* fileName, char* mode); : r : w - a ( , ). ! : r+ , NULL w+ , a+ , . ! b . : rb, wb '. . - mode t, . : wt Slide 8 Keren Kalif 8 " "r" Slide 9 Keren Kalif 9 int fclose(FILE* file); 0 , 1- ( EOF) int fcloseall(); Slide 10 Keren Kalif 10 , : f int fprintf(FILE*, char*,... ); int fputs(char*, FILE*); int fputc(char, FILE*); Slide 11 Keren Kalif 11 (1) 1. #include 2. 3. void main() 4. { 5. FILE* f = fopen("myFile.txt", "w"); 6. int res; 7. if (f == NULL) 8. { 9. printf("Failed opening the file. Exiting!\n"); 10. return; 11. } 12. 13. fprintf(f, "%s %d %lf\n", "KerenK", 28, 99.8); 14. fputs("Hello World!", f); 15. fputc('A', f); 16. 17. fclose(f); 18. } Slide 12 Keren Kalif 12 (2) 1. #include 2. 3. void main() 4. { 5. FILE* f = fopen("myFile.txt", "w"); 6. int res; 7. if (f == NULL) 8. { 9. printf("Failed opening the file. Exiting!\n"); 10. return; 11. } 12. fputs("Hello World!\n", f); 13. fclose(f); 14. 15. f = fopen("myFile.txt", "a"); 16. fputs("And Good Morning!\n", f); 17. fclose(f); 18. } Slide 13 Keren Kalif 13 (3) 1. #include 2. 3. void main() 4. { 5. FILE* f = fopen("myFile.txt", "w"); 6. int res; 7. if (f == NULL) 8. { 9. printf("Failed opening the file. Exiting!\n"); 10. return; 11. } 12. fputs("Hello World!\n", f); 13. fclose(f); 14. 15. f = fopen("myFile.txt", w"); 16. fputs("And Good Morning!\n", f); 17. fclose(f); 18. } ... Slide 14 Keren Kalif 14 , : f int fscanf(FILE*, char*,... ); char* fgets(char*, int n, FILE*); NULL ( ) int fgetc(FILE*); EOF ( 1-) ( ) . \n n-1 Slide 15 Keren Kalif 15 - , : : , .. , , Slide 16 Keren Kalif 16 1. #define SIZE 20 2. void main() 3. { 4. char str[]="Hi", sentence[]="Hello World!", str2[SIZE], sentence2[SIZE]; 5. int num1=6, num2=0; 6. float f1=4.5, f2=0; 7. FILE* f = fopen("myFile.txt", "w"); 8. // check if open file succeeded.. 9. 10. fputs(sentence, f); 11. fprintf(f, "\n%d %f %s\n", num1, f1, str); 12. fclose(f); 13. 14. printf("Before reading from file:\nnum2=%d f2=%f str=|%s| sentence2=|%s|\n", num2, f2, str2, sentence2); 15. f = fopen("myFile.txt", "r"); 16. // check if open file succeeded.. 17. 18. fgets(sentence2, SIZE, f); 19. fscanf(f, "%d %f %s", &num2, &f2, str2); 20. 21. printf("\nAfter reading from file:\nnum2=%d f2=%f str=|%s| sentence2=|%s|\n", num2, f2, str2, sentence2); 22. fclose(f); 23. } Slide 17 Keren Kalif 17 feof int feof (FILE * stream); 0 , - 1- (EOF) Slide 18 Keren Kalif 18 1. void main() 2. { 3. char ch; 4. FILE* fSource, *fDest; 5. fSource = fopen("example.txt", "r"); 6. // check if open file succeeded.. 7. 8. fDest = fopen("exampleCopy.txt", "w"); 9. // check if open file succeeded.. 10. 11. ch = fgetc(fSource); 12. while (!feof(fSource)) // OR: (ch != EOF) 13. { 14. fputc(ch, fDest); 15. ch = fgetc(fSource); 16. } 17. 18. fclose(fSource); // returns 0 since close file succeed 19. fclose(fDest); // returns 0 since close file succeed 20. } fcloseall(); // returns 2 since closed 2 files Slide 19 Keren Kalif 19 1. #include 2. void main() 3. { 4. FILE* f; 5. int num, sum=0, counter=0, rc; 6. 7. f = fopen("numbers.txt", "r"); 8. // check if open succeed... 9. 10. rc = fscanf(f, "%d", &num); 11. while (rc != EOF) //OR: (!feof(f)) 12. { 13. printf("The read numbers is %d\n", num); 14. sum += num; 15. counter++; 16. rc = fscanf(f, "%d", &num); 17. } 18. fclose(f); 19. printf("The average is %f\n", (float)sum/counter); 20. } Slide 20 Keren Kalif 20 , - , - Slide 21 Keren Kalif 21 - 1. #define SIZE 20 2. struct Person 3. { 4. char name[SIZE]; 5. long id; 6. float age; 7. } typedef person_t; 8. void main() 9. { 10. person_t p1={"momo", 1111, 23.5}, p2 = {"gogo", 2222, 24.8}, p3, p4; 11. FILE* f = fopen("persons.txt", "w"); 12. fprintf(f, "%s %ld %.2f\n", p1.name, p1.id, p1.age); 13. fprintf(f, "%s %ld %.2f\n", p2.name, p2.id, p2.age); 14. fclose(f); 15. f = fopen("persons.txt", "r"); 16. fscanf(f, "%s %ld %f\n", p3.name, &p3.id, &p3.age); 17. fscanf(f, "%s %ld %f\n", p4.name, &p4.id, &p4.age); 18. fclose(f); 19. printf("p3: name: %s\t id: %ld\t age: %.2f\n", p3.name, p3.id, p3.age); 20. printf("p4: name: %s\t id: %ld\t age: %.2f\n", p4.name, p4.id, p4.age); 21. } Slide 22 Keren Kalif 22 , , Slide 23 Keren Kalif 23 - (1) 1. struct Person 2. { 3. char name[SIZE]; 4. long id; 5. float age; 6. } typedef person_t; 7. 8. void main() 9. { 10. FILE* f; 11. person_t* personsSource, *personsDest; 12. int sizeSource, sizeDest, i; 13. printf("How many persons? "); 14. scanf("%d", &sizeSource); 15. 16. // allocating the persons array 17. personsSource = (person_t*)malloc(sizeSource*sizeof(person_t)); 18. // reading persons.. 19. for (i=0 ; i < sizeSource ; i++) 20. { 21. printf("Enter name, id and age of person #%d: ", i+1); 22. scanf("%s %ld %f", personsSource[i].name, &personsSource[i].id, &personsSource[i].age); 23. } 24. } Slide 24 Keren Kalif 24 24. f = fopen("persons.txt", "w"); 25. fprintf(f, "%d\n", sizeSource); // writing the size to the file 26. for (i=0 ; i < sizeSource ; i++) // writing each person to the file 27. fprintf(f, "%s %ld %f\n", personsSource[i].name, personsSource[i].id, personsSource[i].age); 28. fclose(f); 29. 30. free(personsSource); // dont forget to free the array!! 31. 32. f = fopen("persons.txt", "r"); 33. fscanf(f, "%d\n", &sizeDest); // reading the size from the file 34. // allocating the new array 35. personsDest = (person_t*)malloc(sizeDest*sizeof(person_t)); 36. // reading each person from the file 37. for (i=0 ; i < sizeDest ; i++) 38. fscanf(f, "%s %ld %f\n", personsDest[i].name, &personsDest[i].id, &personsDest[i].age); 39. fclose(f); 40. 41. printf("There are %d persons in the file:\n", sizeDest); 42. for (i=0 ; i < sizeDest ; i++) 43. printf("Person #%d: Name=%s\t Id=%ld\t Age=%f\n", i+1, 44. personsDest[i].name, personsDest[i].id, personsDest[i].age); 45. 46. free(personsDest); // dont forget to free the array!! 47. } Slide 25 Keren Kalif 25 : int fwrite(const void * ptr, // address of variable to write int size, // size of type int count, // number of elements FILE * stream (; // pointer to the file : ! Slide 26 Keren Kalif 26 : int fread(void* ptr, // address of variable to read into int size, // size of type int count, // number of elements FILE* stream (; // pointer to the file : Slide 27 Keren Kalif 27 - 1. #define SIZE 20 2. struct Person 3. { 4. char name[SIZE]; 5. long id; 6. float age; 7. } typedef person_t; 8. 9. void main() 10. { 11. person_t p1={"momo", 1111, 23.5}, p2 = {"gogo", 2222, 24.8}, p3, p4; 12. FILE* f = fopen("persons.bin", "wb"); 13. fwrite(&p1, sizeof(person_t), 1, f); 14. fwrite(&p2, sizeof(person_t), 1, f); 15. fclose(f); 16. 17. f = fopen("persons.bin", "rb"); 18. fread(&p3, sizeof(person_t), 1, f); 19. fread(&p4, sizeof(person_t), 1, f); 20. fclose(f); 21. 22. printf("p3: name: %s\t id: %ld\t age: %.2f\n", p3.name, p3.id, p3.age); 23. printf("p4: name: %s\t id: %ld\t age: %.2f\n", p4.name, p4.id, p4.age); 24. } ! Slide 28 Keren Kalif 28 (1) 1. #define SIZE 20 2. struct Person 3. { 4. char name[SIZE]; 5. long id; 6. float age; 7. } typedef person_t; 8. 9. void main() 10. { 11. FILE* f; 12. person_t* personsSource, *personsDest; 13. int sizeSource, sizeDest, i; 14. printf("How many persons? "); 15. scanf("%d", &sizeSource); 16. 17. // allocating the persons array 18. personsSource = (person_t*)malloc(sizeSource*sizeof(person_t)); 19. for (i=0 ; i < sizeSource ; i++) // reading persons.. 20. { 21. printf("Enter name, id and age of person #%d: ", i+1); 22. scanf("%s %ld %f", personsSource[i].name, 23. &personsSource[i].id, &personsSource[i].age); 24. } 25. } Slide 29 Keren Kalif 29 25. f = fopen("persons.bin", "wb"); 26. fwrite(&sizeSource, sizeof(int), 1, f); // writing the size to the file 27. // writing all persons to the file 28. fwrite(personsSource, sizeof(person_t), sizeSource, f); 29. fclose(f); 30. free(personsSource); // dont forget to free the array!! 31. 32. f = fopen("persons.bin", "rb"); 33. // reading the size from the file 34. fread(&sizeDest, sizeof(int), 1, f); 35. // allocating the new array 36. personsDest = (person_t*)malloc(sizeDest*sizeof(person_t)); 37. // reading all persons from the file 38. fread(personsDest, sizeof(person_t), sizeDest, f); 39. fclose(f); 40. 41. printf("There are %d persons in the file:\n", sizeDest); 42. for (i=0 ; i < sizeDest ; i++) 43. printf("Person #%d: Name=%s\t Id=%ld\t Age=%f\n", 44. i+1, personsDest[i].name, personsDest[i].id, personsDest[i].age); 45. 46. // dont forget to free the array!! 47. free(personsDest); 48. } Slide 30 Keren Kalif 30 , , : " fwrite, - " , , " (" ). Slide 31 Keren Kalif 31 1. #include 2. #include 3. #incl