Advertisement
rafikamal

Output using fprintf

Feb 3rd, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.     int n, i, number;
  7.     char subject[30];
  8.     FILE *fp;
  9.    
  10.     printf("How many subjects? ");
  11.     scanf("%d", &n);
  12.     getchar();
  13.    
  14.     if((fp = fopen("input.txt", "w")) == NULL)
  15.     {
  16.         printf("Error opening file.\n");
  17.         exit(1);
  18.     }
  19.    
  20.     for(i = 0; i < n; i++)
  21.     {
  22.         printf("Enter the name of subject %d: ", i+1);
  23.         gets(subject);
  24.         printf("Enter the marks of subject %d: ", i+1);
  25.         scanf("%d", &number);
  26.         getchar();
  27.        
  28.         fprintf(fp, "%s %d\n", subject, number);
  29.     }
  30.    
  31.     fclose(fp);
  32.    
  33.     return 0;
  34.    
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement