Search
 
SCRIPT & CODE EXAMPLE
 

C

The fscanf and fprintf functions




/* The fscanf and fprintf functions
We know how to use scanf() function to take input and printf() function to print the output.

In a similar way, we use the fscanf() function to read data from the file and fprintf() function to write data in the file.

Syntax of fscanf function:

fscanf(fptr, "control string", list_of_var);
Where, fptr is a file pointer. The control string contains the input specification like %d for integer or %c for character. list_of_var is the list of variables.

Syntax of fprintf function:

fprintf(fptr, "control string", list_of_var);
Write a program in C to read and write student name, id and score in a file
We will create a student file for this program.

We will use the fprintf() function to write the data in the file and then using the fscanf() function to read the data from the file.*/
#include <stdio.h>

int main(void) {
  // creating a FILE variable
  FILE *fptr;
  
  // integer variable
  int id, score;
  int i, s;
  
  // character variable
  char name[255];
  char n[255];
  
  // open the file in write mode
  fptr = fopen("student", "w");
  
  if (fptr != NULL) {
    printf("File created successfully!
");
  }
  else {
    printf("Failed to create the file.
");
    // exit status for OS that an error occured
    return -1;
  }
  
  // get student detail
  printf("Enter student name: ");
  gets(name);
  printf("Enter student ID: ");
  scanf("%d", &id);
  printf("Enter student score: ");
  scanf("%d", &score);
  
  // write data in file
  fprintf(fptr, "%d %d %s", id, score, name);
  
  // close connection
  fclose(fptr);
  
  // open file for reading
  fptr = fopen("student", "r");
  
  // display detail
  printf("
Student Details:
");
  fscanf(fptr, "%d %d %[^
]s", &i, &s, n);
  printf("ID: %d
", i);
  printf("Name: %s
", n);
  printf("Score: %d
", s);
  
  printf("
End of file.
");
  
  // close connection
  fclose(fptr);
  
  return 0;
}
Comment

PREVIOUS NEXT
Code Example
C :: Fibonacci Series Program. in c 
C :: prime factorization of factorials using c 
C :: inputting an array in c 
C :: equal string c 
C :: c header file example 
C :: adding strings in the list 
C :: c code to grade marks 
C :: flip exis in dataframe 
C :: doble puntero en c 
C :: print command for rust unit-test 
C :: highest common factor algorithm in c 
C :: convert string to int error checking c 
C :: casting in c 
C :: function component with props 
C :: how to run c program from visual studio terminal 
C :: search sorted array c 
C :: sleep in c 
C :: C program to find power of any number 
C :: c char to int 
C :: what is the use of malloc in c 
C :: get docker 
C :: *= operator 
C :: bcd to char c 
C :: Here is a program in C that illustrates the use of fprintf() to write a text file: 
C :: While loop output 
C :: delimter in c 
C :: e sharm card jobkhozo.com 
C :: asasz 
C :: Categorize students according to their marks 
C :: garbage collection and dangling reference 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =