Input using fscanf() Library Function Example

How to write a C program to input using fscanf() library function in C Programming (example)?


Solution:
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n, i, number;
    char subject[30];
    FILE *fp;
 
    if((fp = fopen("input.txt", "r")) == NULL)
    {
   printf("Error opening file.\n");
   exit(1);
    }
 
    fscanf(fp, "%d", &n);
 
    for(i = 0; i < n; i++)
    {
   fscanf(fp, "%s", subject);
   fscanf(fp, "%d", &number);
 
   printf("%s %d\n", subject, number);
    }
 
    fclose(fp);
 
    return 0;
 
}


Learn More :