logo

C fprintf() ו-fscanf()


כתיבת קובץ: פונקציה fprintf().

הפונקציה fprintf() משמשת לכתיבת סט תווים לקובץ. זה שולח פלט מעוצב לזרם.

תחביר:

הורד את autocad 2019 באנגלית Mediafire
 int fprintf(FILE *stream, const char *format [, argument, ...]) 

דוגמא:

 #include main(){ FILE *fp; fp = fopen('file.txt', 'w');//opening file fprintf(fp, 'Hello file by fprintf...
');//writing data into file fclose(fp);//closing file } 

קריאת קובץ: פונקציה fscanf().

הפונקציה fscanf() משמשת לקריאת סט תווים מהקובץ. זה קורא מילה מהקובץ ומחזיר EOF בסוף הקובץ.

תחביר:

 int fscanf(FILE *stream, const char *format [, argument, ...]) 

דוגמא:

 #include main(){ FILE *fp; char buff[255];//creating char array to store data of file fp = fopen('file.txt', 'r'); while(fscanf(fp, '%s', buff)!=EOF){ printf('%s ', buff ); } fclose(fp); } 

תְפוּקָה:

 Hello file by fprintf... 

דוגמה לקובץ C: אחסון מידע עובד

בוא נראה דוגמה לטיפול בקבצים לאחסון פרטי עובדים כפי שהוזן על ידי המשתמש מהמסוף. אנחנו הולכים לשמור את זהות, שם ומשכורת של העובד.

 #include void main() { FILE *fptr; int id; char name[30]; float salary; fptr = fopen('emp.txt', 'w+');/* open for writing */ if (fptr == NULL) { printf('File does not exists 
'); return; } printf('Enter the id
'); scanf('%d', &id); fprintf(fptr, 'Id= %d
', id); printf('Enter the name 
'); scanf('%s', name); fprintf(fptr, 'Name= %s
', name); printf('Enter the salary
'); scanf('%f', &salary); fprintf(fptr, 'Salary= %.2f
', salary); fclose(fptr); }

תְפוּקָה:

jquery את הקליק הזה
 Enter the id 1 Enter the name sonoo Enter the salary 120000 

כעת פתח את הקובץ מהספרייה הנוכחית. עבור מערכת ההפעלה Windows, עבור אל ספריית TCin, תראה קובץ emp.txt. זה יכלול את המידע הבא.

emp.txt

 Id= 1 Name= sonoo Salary= 120000