Formatted Input and Output

Formatted I/O includes fprintf(), fscanf() functions. Those functions we are going to discuss in this blog.

fprintf()

This function is used to write multiple variables of multiple data types into a file. Suppose if we want to write an integer array and one float variable into a file then we can use this function.

fprintf(<file pointer name>,”<control string for var 1, control string for var 2,… control string for var n>”,<var 1 name, var 2 name,… var n name>);

Suppose we want to make file pointer fp1 to point to a text file having the name f1.txt. and we want to write “ABCD” and 10 into file f1.txt. So for ease, we have stored “ABCD” in variable c(character array) and 10 in the variable i (integer variable). Then below is an example of fprintf syntax. Control string related to “ABCD” is %s and related to 10 is %d.

int i=10;
char c[10]=”ABCD”;
fprintf(fp1,“%s %d”,c,i);

fgets()

This function is used to read multiple variables of multiple data types from a file. Suppose if we want to read an integer array and one float variable from a file then we can use this function.

fscanf(<file pointer name>,”<control string for var 1, control string for var 2,… control string for var n>”,&<var 1 name>, &<var 2 name>,… &<var n name>);

& sign is not used for string 

As per the previous example, we want to make file pointer fp1 to point to a text file having the name f1.txt. and we want to read string and integer from file f1.txt.
We have taken variable c1(character array) to store string and variable i1 (integer variable) to store an integer. Then below is an example of fscanf syntax. Control string related to string is %s and related to integer is %d.

int i1;
char c1[10];
fprintf(fp1,“%s %d”,c1,&i); //as c1 is for string then we are taking c1 instead of &c1

Example program structure to understand the use of fprintf and fscanf

In the below example, f1 is a file name and fp1 is a file pointer. First, we have used fopen with mode w+ because we want to write using fprintf and read using fscanf. Then we have used fprintf to write the string “ABCDEF” (which is stored in array c) and integer 10 (which is stored in variable i)in the f1 file. To read from the start of the file, we are using the rewind function. We are reading strings and integers from the file. So we have used c1 and i1 to store those data respectively. And the values of both are printed at the output.

#include<stdio.h>
#include<conio.h>
void main()
{
  char c[10]="ABCD",c1[10];
  int i=10,i1;
  FILE *fp1;
  clrscr();
  fp1=fopen("f1.txt","w+");
  fprintf(fp1,"%s %d",c,i);
  rewind(fp1);
  fscanf(fp1,"%s %d",c1,&i1);
  printf("string = %s , integer = %d ",c1,i1);
  fclose(fp1);
  getch();
}

Output:

Learning from this blog:

  1. What is the need of fprintf?
  2. The syntax for fprintf.
  3. What is needed for fscanf?
  4. The syntax for fscanf.
  5. Write a program using fprintf and fscanf.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments