Text file fscanffile pointer fprintffile pointer include stdio

  • Slides: 13
Download presentation

Text file fscanf(file pointer, ”…”, &…); fprintf(file pointer, ”…”, …); #include <stdio. h> int

Text file fscanf(file pointer, ”…”, &…); fprintf(file pointer, ”…”, …); #include <stdio. h> int main(){ char c; FILE* fid=fopen("c: \temp\speed 1. dat", "r"); if (!fid) return 1; while(1) { כתיבה נוספות / פעולות קריאה fscanf(fid, "%c", &c); . ימשיכו מהמקום בו עצרנו printf("%c", c); } fclose(fid); return 0; }. © כל הזכויות שמורות. נכתב ע"י יעל ארז 7

. eof בסוף הקובץ נדלק דגל . feof(file pointer) ניתן לבדיקה ע"י #include <stdio.

. eof בסוף הקובץ נדלק דגל . feof(file pointer) ניתן לבדיקה ע"י #include <stdio. h> int main(){ char c; FILE* fid=fopen("c: \temp\speed 1. dat", "r"); if (!fid) return 1; while(1) { fscanf(fid, "%c", &c); if (feof(fid)) break; printf("%c", c); } fclose(fid); return 0; . © כל הזכויות שמורות. נכתב ע"י יעל ארז } 8

Binary file fread(&buffer , record size , number of records , file pointer); fwrite(&buffer

Binary file fread(&buffer , record size , number of records , file pointer); fwrite(&buffer , record size , number of records , file pointer); : כתיבת מחרוזת לקובץ בינארי #include <stdio. h> #include <string. h> int main() { FILE *fp; char c[] = "this is a binary file"; fp = fopen(“c: \temp\file. bin", "wb"); fwrite(c, 1, strlen(c) + 1 , fp); fclose(fp); return 0; }. © כל הזכויות שמורות. נכתב ע"י יעל ארז 10

fseek(file pointer, offset, origin); Long offset = ftell(file pointer); . ניתן להזיז את מיקומנו

fseek(file pointer, offset, origin); Long offset = ftell(file pointer); . ניתן להזיז את מיקומנו בקובץ Constant Reference position SEEK_SET Beginning of file SEEK_CUR Current position of the file pointer SEEK_END End of file * . ניתן לקבל את מיקומנו בקובץ . © כל הזכויות שמורות. נכתב ע"י יעל ארז 11

/* ftell example : getting size of a file (in bytes)*/ #include <stdio. h>

/* ftell example : getting size of a file (in bytes)*/ #include <stdio. h> int main () { FILE * p. File; long size; p. File = fopen (“c: \temp\file. bin", "rb"); if (!p. File) printf("Error opening filen"); else { fseek (p. File, 0, SEEK_END); size=ftell (p. File); fclose (p. File); printf ("Size of myfile. bin: %ld bytes. n", size); } return 0; }. © כל הזכויות שמורות. נכתב ע"י יעל ארז 12

#include <stdio. h> #include <stdlib. h> קריאת מחרוזת int main() { מקובץ בינארי FILE

#include <stdio. h> #include <stdlib. h> קריאת מחרוזת int main() { מקובץ בינארי FILE *fp; : והדפסתה למסך char* buffer = NULL; long size; fp = fopen(“c: \temp\file. bin", "rb"); if (!fp) return 1; fseek (fp, 0, SEEK_END); size=ftell (fp); fseek (fp, 0, SEEK_SET); buffer = malloc(size); if (!buffer) return 2; fread(buffer, 1, size, fp); fclose(fp); printf("%s", buffer); free(buffer); return 0; . © כל הזכויות שמורות. נכתב ע"י יעל ארז 13 }