Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //file: dataread.c
- /*
- * This function is used to scan the data files to arrange the
- * data into a 2D array Also it will indirectly pass the start
- * and end distances between cities. */
- #include "dataread.h"
- int main()
- {
- //initialize variables
- int distance[15], j = 0, i = 0;
- char city1[15][20], city2[15][20];
- //creating file pointers
- FILE *fin1, *fin2, *fin3;
- //opening file leg1
- if((fin1 = fopen(leg1, "r")) == NULL)
- printf("ERROR: cannot open %s\n", leg1);
- //read and scan leg1 file
- for(; i < 3; i++)
- fscanf(fin1, "%s %s %d", &city1[i][j], &city2[i][j], &distance[i]);
- //close the files
- fclose(fin1);
- //opening file leg2
- if((fin2 = fopen(leg2, "r")) == NULL)
- printf("ERROR: cannot open %s\n", leg2);
- //read and scan leg2 file
- for(; i < 12; i++)
- fscanf(fin2, "%s %s %d", &city1[i][j], &city2[i][j], &distance[i]);
- //close the files
- fclose(fin2);
- //opening file leg3
- if((fin3 = fopen(leg3, "r")) == NULL)
- printf("ERROR: cannot open %s\n", leg3);
- //read and scan leg3 file
- for(; i < 15; i++)
- fscanf(fin1, "%s %s %d", &city1[i][j], &city2[i][j], &distance[i]);
- //close the files
- fclose(fin3);
- //test print the to check if arrays were properly saved
- for(int x = 0; x < 15; x++)
- {
- for(int k = 0; k < 20; k++ )
- {
- if (city1[x][k] == '\0')
- break;
- printf("%c", city1[x][k]);
- }
- printf(" ");
- for(int j = 0; j < 20; j++ )
- {
- if (city2[x][j] == '\0')
- break;
- printf("%c", city2[x][j]);
- }
- printf(" %d\n", distance[x]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement