Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. //file: dataread.c
  2.  
  3. /*
  4. * This function is used to scan the data files to arrange the
  5. * data into a 2D array Also it will indirectly pass the start
  6. * and end distances between cities. */
  7.  
  8. #include "dataread.h"
  9.  
  10. int main()
  11. {
  12. //initialize variables
  13. int distance[15], j = 0, i = 0;
  14. char city1[15][20], city2[15][20];
  15.  
  16. //creating file pointers
  17. FILE *fin1, *fin2, *fin3;
  18.  
  19. //opening file leg1
  20. if((fin1 = fopen(leg1, "r")) == NULL)
  21. printf("ERROR: cannot open %s\n", leg1);
  22.  
  23. //read and scan leg1 file
  24. for(; i < 3; i++)
  25. fscanf(fin1, "%s %s %d", &city1[i][j], &city2[i][j], &distance[i]);
  26.  
  27. //close the files
  28. fclose(fin1);
  29.  
  30. //opening file leg2
  31. if((fin2 = fopen(leg2, "r")) == NULL)
  32. printf("ERROR: cannot open %s\n", leg2);
  33.  
  34. //read and scan leg2 file
  35. for(; i < 12; i++)
  36. fscanf(fin2, "%s %s %d", &city1[i][j], &city2[i][j], &distance[i]);
  37.  
  38. //close the files
  39. fclose(fin2);
  40.  
  41. //opening file leg3
  42. if((fin3 = fopen(leg3, "r")) == NULL)
  43. printf("ERROR: cannot open %s\n", leg3);
  44.  
  45. //read and scan leg3 file
  46. for(; i < 15; i++)
  47. fscanf(fin1, "%s %s %d", &city1[i][j], &city2[i][j], &distance[i]);
  48.  
  49. //close the files
  50. fclose(fin3);
  51.  
  52. //test print the to check if arrays were properly saved
  53. for(int x = 0; x < 15; x++)
  54. {
  55. for(int k = 0; k < 20; k++ )
  56. {
  57. if (city1[x][k] == '\0')
  58. break;
  59. printf("%c", city1[x][k]);
  60. }
  61. printf(" ");
  62.  
  63. for(int j = 0; j < 20; j++ )
  64. {
  65. if (city2[x][j] == '\0')
  66. break;
  67.  
  68. printf("%c", city2[x][j]);
  69. }
  70. printf(" %d\n", distance[x]);
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement