Output using fprintf() library function example program

How to write a C program Output using fprintf() library function in C programming (example) ?


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

int main()
{
    int n, i, number;
    char subject[30];
    FILE *fp;
 
    printf("How many subjects? ");
    scanf("%d", &n);
    getchar();
 
    if((fp = fopen("input.txt", "w")) == NULL)
    {
   printf("Error opening file.\n");
   exit(1);
    }
 
    for(i = 0; i < n; i++)
    {
   printf("Enter the name of subject %d: ", i+1);
   gets(subject);
   printf("Enter the marks of subject %d: ", i+1);
   scanf("%d", &number);
   getchar();
 
   fprintf(fp, "%s %d\n", subject, number);
    }
 
    fclose(fp);
 
    return 0;
 
}


Learn More :