I don't understand why the key is correct in the first instance, but defaulted to the last known value in the second instance:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINES (int)1E03
const int MAX_LINE_SIZE = 30;
const char delim[2] = "\t";
struct temperature {
char *date;
char *country;
char *location;
char *key;
float temp;
};
struct temperature temps[MAX_LINES];
/* compare function for sorting purposes, note that this sorting relies on there not being a duplicate */
int compare (const void *a, const void *b) {
return strcmp((*(struct temperature*)a).key, (*(struct temperature*)b).key);
}
/* main function */
int main (int argc, char *argv[]) {
char line[MAX_LINE_SIZE];
FILE *f = NULL;
f = fopen(argv[1], "r");
int n = 0;
char key[100];
while (fgets(line, MAX_LINE_SIZE, f)) {
temps[n].date = strtok(strdup(line), delim);
if (temps[n].date != NULL) {
temps[n].country = strtok(NULL, delim);
temps[n].location = strtok(NULL, delim);
temps[n].temp = atof(strtok(NULL, delim));
// create a key (string) for sorting purposes: first by country, then by location, then by date
strcat(strcat(strcpy(key, temps[n].country), temps[n].location), temps[n].date);
temps[n].key = key;
printf("n: %d\t%s\n", n, temps[n].key);
n++;
}
}
fclose(f);
printf("Number of lines read: %d\n", n);
for (int i = 0; i < n; i++) { printf("i: %d\t%s\t%s\t%s\t%.2f\t%s\n", i, temps[i].date, temps[i].country, temps[i].location, temps[i].temp, temps[i].key); }
// qsort(temps, n, sizeof(struct temperature), compare);
// for (int i = 0; i < n; i++) { printf("i: %d\t%s\t%s\t%s\t%.2f\t%s\n", i, temps[i].date, temps[i].country, temps[i].location, temps[i].temp, temps[i].key); }
return 0;
}
Input file:
2021-11-09 UK ACK 16.3
2021-11-09 UK AOO 15.6
2021-11-09 UK AGE 22.4
2021-11-09 UK ABI 11.8
2021-11-09 UK APA 20.7
2021-11-09 UK ARG 28.5
2021-11-09 UK COF 21.6
2021-11-10 UK ACK 15.3
2021-11-10 UK AOO 14.6
2021-11-10 UK AGE 11.8
2021-11-10 UK ABI 22.4
2021-11-10 UK APA 29.6
2021-11-10 UK ARG 28.4
2021-11-10 UK COF 14.1
The output is as follows, where the key is correct in the first instance, but incorrect in the second instance:
n: 0 UKACK2021-11-09
n: 1 UKAOO2021-11-09
n: 2 UKAGE2021-11-09
n: 3 UKABI2021-11-09
n: 4 UKAPA2021-11-09
n: 5 UKARG2021-11-09
n: 6 UKCOF2021-11-09
n: 7 UKACK2021-11-10
n: 8 UKAOO2021-11-10
n: 9 UKAGE2021-11-10
n: 10 UKABI2021-11-10
n: 11 UKAPA2021-11-10
n: 12 UKARG2021-11-10
n: 13 UKCOF2021-11-10
Number of lines read: 14
i: 0 2021-11-09 UK ACK 16.30 UKCOF2021-11-10
i: 1 2021-11-09 UK AOO 15.60 UKCOF2021-11-10
i: 2 2021-11-09 UK AGE 22.40 UKCOF2021-11-10
i: 3 2021-11-09 UK ABI 11.80 UKCOF2021-11-10
i: 4 2021-11-09 UK APA 20.70 UKCOF2021-11-10
i: 5 2021-11-09 UK ARG 28.50 UKCOF2021-11-10
i: 6 2021-11-09 UK COF 21.60 UKCOF2021-11-10
i: 7 2021-11-10 UK ACK 15.30 UKCOF2021-11-10
i: 8 2021-11-10 UK AOO 14.60 UKCOF2021-11-10
i: 9 2021-11-10 UK AGE 11.80 UKCOF2021-11-10
i: 10 2021-11-10 UK ABI 22.40 UKCOF2021-11-10
i: 11 2021-11-10 UK APA 29.60 UKCOF2021-11-10
i: 12 2021-11-10 UK ARG 28.40 UKCOF2021-11-10
i: 13 2021-11-10 UK COF 14.10 UKCOF2021-11-10
Using gcc. The key defaults to "UKCOF2021-11-10" in all instances and I do not know why and it prevents me from using qsort. Can anyone point out why this is happening?