Formatted File Input and Output in C Using fscanf() and fprintf()
Introduction to Formatted File Input and Output
In C language, the fscanf() and fprintf() functions are used to read and write formatted data in a file. These functions work similarly to scanf() and printf(), but instead of reading from the keyboard or displaying output on the screen, they perform input and output operations on files.
C language में fscanf() और fprintf() functions का उपयोग किसी file में formatted data को read और write करने के लिए किया जाता है। ये functions scanf() और printf() की तरह कार्य करते हैं, लेकिन keyboard से input लेने या screen पर output दिखाने के बजाय files पर input और output operations perform करते हैं।
Both functions are declared in the <stdio.h> header file.
दोनों functions <stdio.h> header file में declared होते हैं।
1. fprintf() Function in C
The fprintf() function is used to write formatted data into a file. It can write different types of data, such as integers, floating-point numbers and strings, using format specifiers.
fprintf() function का उपयोग किसी file में formatted data write करने के लिए किया जाता है। इसकी सहायता से format specifiers का उपयोग करके integer, floating-point numbers और strings जैसे अलग-अलग प्रकार के data को file में लिखा जा सकता है।
Syntax
int fprintf(FILE *fp, const char *format, ...);Parameters:
fp: Pointer to the file opened in a suitable writing mode.format: Format string containing format specifiers such as%d,%f,%cand%s....: Values that are to be written into the file.
Return Value:
The fprintf() function returns the number of characters written successfully. It returns a negative value if a writing error occurs.
fprintf() function सफलतापूर्वक write किए गए characters की संख्या return करता है। यदि writing के दौरान कोई error आती है, तो यह negative value return करता है।
Example: C Program to Write Formatted Data into a File Using fprintf()
#include <stdio.h>int main(void){FILE *fp;int rollno = 101;char name[] = "Ravi";float marks = 85.50f;fp = fopen("student.txt", "w");if (fp == NULL){printf("Unable to open file.\n");return 1;}if (fprintf(fp, "%d %s %.2f\n",rollno, name, marks) < 0){printf("Error writing data to file.\n");fclose(fp);return 1;}printf("Student data written successfully.\n");fclose(fp);return 0;}
Output:
Student data written successfully.File Content (student.txt):
101 Ravi 85.50In this program, the file student.txt is opened in write mode. The fprintf() function writes the roll number, name and marks into the file using the format specifiers %d, %s and %.2f. Finally, the file is closed using fclose().
इस program में student.txt file को write mode में open किया जाता है। fprintf() function %d, %s और %.2f format specifiers की सहायता से roll number, name और marks को file में write करता है। अंत में fclose() function की सहायता से file को close कर दिया जाता है।
2. fscanf() Function in C
The fscanf() function is used to read formatted data from a file. It reads data according to the specified format string and stores the values in the given variables.
fscanf() function का उपयोग किसी file से formatted data read करने के लिए किया जाता है। यह दिए गए format string के अनुसार data read करता है और प्राप्त values को निर्धारित variables में store करता है।
Syntax
int fscanf(FILE *fp, const char *format, ...);Parameters:
fp: Pointer to the file opened in reading mode.format: Format string containing format specifiers such as%d,%f,%cand%s....: Addresses of variables in which the read values are stored.
Return Value:
The fscanf() function returns the number of input items successfully assigned to variables. It returns EOF if an input failure occurs before the first conversion is completed.
fscanf() function सफलतापूर्वक variables में assign किए गए input items की संख्या return करता है। यदि पहली conversion पूरी होने से पहले input failure हो जाता है, तो यह EOF return करता है।
Example: C Program to Read Formatted Data from a File Using fscanf()
#include <stdio.h>int main(void){FILE *fp;int rollno;char name[30];float marks;fp = fopen("student.txt", "r");if (fp == NULL){printf("Unable to open file.\n");return 1;}if (fscanf(fp, "%d %29s %f",&rollno, name, &marks) == 3){printf("Student Details\n");printf("Roll Number: %d\n", rollno);printf("Name: %s\n", name);printf("Marks: %.2f\n", marks);}else{printf("Unable to read student data.\n");}fclose(fp);return 0;}
Assume the file student.txt contains:
101 Ravi 85.50Output:
Student DetailsRoll Number: 101Name: RaviMarks: 85.50
In this program, the file student.txt is opened in read mode. The fscanf() function reads the roll number, name and marks from the file and stores them in the corresponding variables. The address operator (&) is used with numeric variables, while the character array name is passed directly.
इस program में student.txt file को read mode में open किया जाता है। fscanf() function file से roll number, name और marks read करके संबंधित variables में store करता है। Numeric variables के साथ address operator (&) का उपयोग किया जाता है, जबकि character array का नाम सीधे pass किया जाता है।
3. C Program to Write and Read Student Records Using fprintf() and fscanf()
The following program demonstrates how to write student details into a file using fprintf() and read them back using fscanf(). It stores the roll number, name and marks of multiple students.
निम्न program में fprintf() function की सहायता से students की details file में write की जाती हैं तथा fscanf() function की सहायता से उन्हें दोबारा read किया जाता है। इसमें कई students के roll number, name और marks store किए जाते हैं।
Program
#include <stdio.h>int main(void){FILE *fp;int i, n;int rollno;char name[30];float marks;printf("Enter number of students: ");if (scanf("%d", &n) != 1 || n <= 0){printf("Invalid number of students.\n");return 1;}fp = fopen("students.txt", "w");if (fp == NULL){printf("Unable to open file for writing.\n");return 1;}for (i = 0; i < n; i++){printf("\nEnter roll number, name and marks:\n");if (scanf("%d %29s %f",&rollno, name, &marks) != 3){printf("Invalid student data.\n");fclose(fp);return 1;}if (fprintf(fp, "%d %s %.2f\n",rollno, name, marks) < 0){printf("Error writing data.\n");fclose(fp);return 1;}}fclose(fp);fp = fopen("students.txt", "r");if (fp == NULL){printf("Unable to open file for reading.\n");return 1;}printf("\nStudent Records:\n");printf("Roll No. Name Marks\n");while (fscanf(fp, "%d %29s %f",&rollno, name, &marks) == 3){printf("%-9d %-6s %.2f\n",rollno, name, marks);}if (ferror(fp)){printf("Error reading file.\n");fclose(fp);return 1;}fclose(fp);return 0;}
Sample Input:
Enter number of students: 3Enter roll number, name and marks:101 Ravi 85.50Enter roll number, name and marks:102 Amit 78.00Enter roll number, name and marks:103 Neha 92.50
Output:
Student Records:Roll No. Name Marks101 Ravi 85.50102 Amit 78.00103 Neha 92.50
In this program, the number of students is entered by the user. The fprintf() function writes each student's details into students.txt. After writing all records, the file is closed and reopened in read mode. The fscanf() function reads each record, and the while loop displays the records until no complete record remains.
इस program में user से students की संख्या input की जाती है। fprintf() function प्रत्येक student की details को students.txt file में write करता है। सभी records write करने के बाद file को close करके read mode में दोबारा open किया जाता है। fscanf() function प्रत्येक record को read करता है और while loop सभी records को display करता है।
Note: In this example, each student's name is a single word because %s stops reading at whitespace. To store full names containing spaces, a different record format and suitable reading method should be used.
इस example में प्रत्येक student का name केवल एक word है, क्योंकि %s whitespace मिलने पर reading रोक देता है। यदि पूरा नाम spaces के साथ store करना हो, तो उसके लिए अलग record format और उपयुक्त reading method का उपयोग करना चाहिए।
Difference Between fscanf() and fprintf()
| fscanf() | fprintf() |
|---|---|
| Used to read formatted data from a file. | Used to write formatted data into a file. |
Similar to scanf(). | Similar to printf(). |
| Reads values and stores them in variables. | Writes formatted values into a file. |
Returns the number of input items assigned, or EOF on an early input failure. | Returns the number of characters written, or a negative value on error. |
fscanf() | fprintf() |
|---|---|
| File से formatted data read करने के लिए उपयोग होता है। | File में formatted data write करने के लिए उपयोग होता है। |
scanf() के समान कार्य करता है। | printf() के समान कार्य करता है। |
| Values read करके variables में store करता है। | Formatted values को file में write करता है। |
Assigned input items की संख्या या शुरुआती input failure पर EOF return करता है। | लिखे गए characters की संख्या या error होने पर negative value return करता है। |
Important Points About fscanf() and fprintf()
Both functions are declared in the
<stdio.h>header file.दोनों functions
<stdio.h>header file में declared होते हैं।fprintf()is used to write formatted data into a file.fprintf()का उपयोग file में formatted data write करने के लिए किया जाता है।fscanf()is used to read formatted data from a file.fscanf()का उपयोग file से formatted data read करने के लिए किया जाता है।Common format specifiers include
%dfor integers,%ffor floating-point values,%cfor characters and%sfor strings.सामान्य format specifiers में integers के लिए
%d, floating-point values के लिए%f, characters के लिए%cऔर strings के लिए%sशामिल हैं।Always use appropriate buffer-width limits with
%swhile reading into character arrays, such as%29sforchar name[30].Character arrays में data read करते समय
%sके साथ उचित width limit का उपयोग करें, जैसेchar name[30]के लिए%29s।Always check the return value of
fscanf()to ensure that the expected number of input items was read successfully.यह सुनिश्चित करने के लिए कि अपेक्षित input items सफलतापूर्वक read हुए हैं, हमेशा
fscanf()के return value को check करना चाहिए।Always check for file-opening and file-writing errors, and close opened files using
fclose().File open करने और write करने से संबंधित errors को check करें तथा opened files को
fclose()की सहायता से close करें।
The fscanf() and fprintf() functions are useful for formatted file input and output in C. They allow programmers to store and retrieve different types of data, such as student records, employee details and other structured information, using format specifiers.
C language में fscanf() और fprintf() functions formatted file input और output के लिए उपयोगी हैं। इनकी सहायता से programmers format specifiers का उपयोग करके student records, employee details और अन्य structured information जैसे अलग-अलग प्रकार के data को file में store और retrieve कर सकते हैं।
Comments
Post a Comment