Advertisement
rafikamal

Input using fscanf

Feb 3rd, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.48 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.     if((fp = fopen("input.txt", "r")) == NULL)
  11.     {
  12.         printf("Error opening file.\n");
  13.         exit(1);
  14.     }
  15.    
  16.     fscanf(fp, "%d", &n);
  17.    
  18.     for(i = 0; i < n; i++)
  19.     {
  20.         fscanf(fp, "%s", subject);
  21.         fscanf(fp, "%d", &number);
  22.        
  23.         printf("%s %d\n", subject, number);
  24.     }
  25.    
  26.     fclose(fp);
  27.    
  28.     return 0;
  29.    
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement