fork(2) download
  1. #include <stdio.h>
  2.  
  3. typedef struct location
  4. {
  5. double lat;
  6. double longt;
  7. }location_t;
  8.  
  9. typedef struct geo
  10. {
  11. char name[64];
  12. char animal[64];
  13. location_t loc;
  14. } geo_t;
  15.  
  16. int main()
  17. {
  18.  
  19. FILE *fp = NULL;
  20. int i;
  21. /* Commneted fopen - Reading from stdin
  22.   if((fp = fopen("file.txt", "r")) == NULL)
  23.   {
  24.   printf ("Can't open file file.txt\n");
  25.   exit(1);
  26.   } */
  27. /* Array of Structure */
  28.  
  29. #define NO_ENTRIES 5 /* No of entries in your file */
  30.  
  31. geo_t arr[NO_ENTRIES];
  32. for(i = 0; i< NO_ENTRIES; i++) {
  33. //Reading from fp.
  34. //fscanf(fp, " %s %s %lf %lf", arr[i].name, arr[i].animal, &arr[i].loc.lat, &arr[i].loc.longt);
  35.  
  36. //Reading from stdin.
  37. fscanf(stdin, " %s %s %lf %lf", arr[i].name, arr[i].animal, &arr[i].loc.lat, &arr[i].loc.longt);
  38. }
  39.  
  40. printf("After Reading\n\n\n");
  41. for(i = 0; i< NO_ENTRIES; i++)
  42. printf(" %s %s %f %f\n", arr[i].name, arr[i].animal, arr[i].loc.lat, arr[i].loc.longt);
  43. return 0;
  44. }
Success #stdin #stdout 0s 2252KB
stdin
1 tiger 100.43 101.4343
2 lion  102.324232 103.56456
3 lion1  103.111 106.323
4 lion2  104 107
5 lion3  105 108
stdout
After Reading


 1 tiger 100.430000 101.434300
 2 lion 102.324232 103.564560
 3 lion1 103.111000 106.323000
 4 lion2 104.000000 107.000000
 5 lion3 105.000000 108.000000