hi
im having a bit of a problem with a task i have.
i have 2 csv files in which a list of countries and data is stored, ive managed to read the first csv into an array without a problem, the problem comes when i come to read in the second csv
what i want to do is run thru the second csv file and read in a figure from it for the corresponding countrty already stored in an array, however the country names are not all the same in the 2 files.
therefore what ive decided to do is do a strcmp on the country name and only add the data where the country and year matches, this is an example of the code i have so far
Code:typedef struct { int year; // year for which data exists double pop; // Population that year (in millions) double bot; double botPerHead; } Data; typedef struct { char countryName[50]; // maximum length of name char countryCode[4]; // UN assigned 3 character country code Data yearlyData[11]; // max. 12 year’s data (1995 – 2006) } Country; int main(void) { FILE *fppop; FILE *fpbot; Country countries[250]; int i = -1; int dataYear; char input[1500]; char *p; char previousName[50] = ""; // read in pop file fppop = fopen("un-country-pop.csv", "r"); if (fppop == NULL) { puts("Error opening file"); exit(EXIT_FAILURE); } while (!feof(fppop)) { fgets(input, 1500, fppop); if (input[0] == '#') continue; p = strtok (input,","); if (strcmp(p, previousName) != 0) { // first time we've seen this country i++; // copy country name to countries[i]. strcpy(previousName, p); } strcpy(countries[i].countryName, p); p = strtok (NULL,","); strcpy(countries[i].countryCode, p); strtok(NULL,","); strtok(NULL,","); strtok(NULL,","); p = strtok(NULL,","); // year string dataYear = atoi(p); countries[i].yearlyData[2006 - dataYear].year = atoi(p); p = strtok(NULL,","); //pop countries[i].yearlyData[2006 - dataYear].pop = atof(p); } // read in bot file fpbot = fopen("un-trade-balance.csv", "r"); if (fpbot == NULL) { puts("Error opening file"); exit(EXIT_FAILURE); } i = 0; while (!feof(fpbot)) { fgets(input, 1500, fpbot); if (input[0] == '#') continue; p = strtok (input,","); if (strcmp(p, countries[i].countryName) != 0) { i++; } strtok(NULL,","); strtok(NULL,","); strtok(NULL,","); strtok(NULL,","); p = strtok(NULL,","); // year dataYear = atoi(p); p = strtok(NULL,","); // bop printf("%d\n",i); countries[i].yearlyData[2006 - dataYear].bot = atof(p); } }



LinkBack URL
About LinkBacks




I used to be an adventurer like you... then I took an arrow to the knee.