Movatterモバイル変換


[0]ホーム

URL:


OverIQ.com

  1. Home
  2. C Programming Tutorial
  3. fprintf() Function in C

fprintf() Function in C

Last updated on July 27, 2020


Formatted File Input and Output#

Up to this point, we have seen how to read and write characters and string to and from the file. In the real world, the data consists of many different types. In this chapter, we will learn how we can input and output data of different types in a formatted way. We use formatted input and output when we want to read or write data in a particular format.

fprintf() function#

Syntax:int fprintf(FILE *fp, const char *format [, argument, ...] );

Thefprintf() function is same asprintf() but instead of writing data to the console, it writes formatted data into the file. Almost all the arguments offprintf() function is same asprintf() function except it has an additional argument which is a file pointer to the file where the formatted output will be written. On success, it returns the total number of characters written to the file. On error, it returnsEOF.

The following program demonstrates how to usefprintf() function.

 1 2 3 4 5 6 7 8 9101112131415161718192021222324252627282930313233343536373839404142434445
#include<stdio.h>#include<stdlib.h>intmain(){FILE*fp;charname[50];introll_no,chars,i,n;floatmarks;fp=fopen("records.txt","w");if(fp==NULL){printf("Error opening file\n");exit(1);}printf("Testing fprintf() function:\n\n");printf("Enter the number of records you want to enter: ");scanf("%d",&n);for(i=0;i<n;i++){fflush(stdin);printf("\nEnter the details of student %d\n\n",i+1);printf("Enter name of the student: ");gets(name);printf("Enter roll no: ");scanf("%d",&roll_no);printf("Enter marks: ");scanf("%f",&marks);chars=fprintf(fp,"Name: %s\t\tRoll no: %d\t\tMarks: %.2f\n",name,roll_no,marks);printf("\n%d characters successfully written to the file\n\n",chars);}fclose(fp);return0;}

Expected Output:

 1 2 3 4 5 6 7 8 910111213141516171819202122232425262728293031323334353637383940414243
Testing fprintf() function:Enter the number of records you want to enter: 5Enter the details of student 1Enter name of the student: TinaEnter roll no: 1Enter marks: 4537 characters successfully written to the fileEnter the details of student 2Enter name of the student: NinaEnter roll no: 5Enter marks: 8937 characters successfully written to the fileEnter the details of student 3Enter name of the student: TimEnter roll no: 2Enter marks: 4936 characters successfully written to the fileEnter the details of student 4Enter name of the student: JimEnter roll no: 8Enter marks: 4136 characters successfully written to the fileEnter the details of student 5Enter name of the student: KingEnter roll no: 9Enter marks: 5937 characters successfully written to the file

How it works:

In line 6, a structure pointer variable fp is declared of type struct FILE.

In line 7, an array of characters name of size 50 is declared.

In line 8, four variables namely roll_no, chars, i and n of type int are declared.

In line 9, a variable marks of type float is declared.

In line 11, fopen() function is called with two arguments namely "records.txt" and "w". On success, it returns a pointer to file records.txt and opens the file records.txt in write-only mode. On failure, it returns NULL.

In line 13-17, if statement is used to test the value of fp. If it is NULL, printf() statement prints the error message and program terminates. Otherwise, the program continues with the statement following the if statement.

In line 19, a printf() statement prints "Testing fprintf() function: \n\n" to the console.

In line 21-22, the program asks the user to enter the number of students whose records he wants to enter.

In lines 24-41, a for loop asks the user to enter three pieces of information name, roll_no and marks of the respective students. In line 26, we are flushing (removing) the contents of the standard input, this line is necessary otherwise gets() function in line 30 will read the newline character (entered while asking the number of students) and will not wait for the user to enter the name of the student.

In line 38, fprintf() function is called along with 5 arguments to write formatted data to the file. If the data has been written successfully to the file, it returns the number of characters written to the file, which is then assigned to variable chars. In line 40, a printf() statement prints the total number of characters written to the file by the previous call of fprintf() function. The loop will keep asking for more records of students until i < n. As soon as n becomes greater than i, the control comes out of the for loop.

In line 43, fclose() function closes the file.


Load Comments


C Programming Tutorial

Recent Posts

x

[8]ページ先頭

©2009-2025 Movatter.jp