#include #include #include #include #include static unsigned int hotel[4]; static FILE *data; /** * Retrieves the next ride details from the data file. * * WARNING: this function is not thread safe. It must be called within * the context of a mutex or some other locking mechanism. * * @param[out] src Index of which hotel to pick up passengers. * @param[out] dest Index of destination hotel to drop off passengers. * @param[out] num Number of passengers to deliver. * @param[out] driving Time spent driving, in seconds. * * @return 1 if data file parsed and ride details written to * parameters, 0 if no rides remaining in data file, or -1 on error. */ static int get_ride(unsigned int *src, unsigned int *dest, unsigned int *num, unsigned int *driving) { char line[20]; line[0] = '#'; while (line[0] == '#') { if (fgets(line, sizeof(line), data) == NULL) { if (ferror(data)) { return -1; } return 0; } } if (line[0] == '\0') { return 0; } else if (strlen(line) < 5) { return -1; } *src = line[0] - '0'; *dest = line[1] - '0'; *driving = line[2] - '0'; *num = atoi(line + 3); return 1; } /* YOUR CODE HERE */ int main(int argc, char *argv[]) { if (argc < 3) { fprintf(stderr, "Need to specify data filename and number of cabs.\n"); exit(1); } if ((data = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "Could not open `%s' for reading.\n", argv[1]); exit(1); } int num_cabs = atoi(argv[2]); hotel[0] = hotel[1] = hotel[2] = hotel[3] = 10; /* YOUR CODE HERE */ printf("End of day check:\n"); printf(" Hotel 0: %d 1: %d 2: %d 3: %d\n", hotel[0], hotel[1], hotel[2], hotel[3]); fclose(data); return 0; }