Search in a .csv file in C

0

Hello! I'm new to programming, and for a college job, I have to create a project in which I have to use .csv files. The program itself compiles, but I know I have an error in an if function, and I can not identify it. I searched for examples, but found nothing. Me and a friend also came to ask similar questions on the English site, but they were quite rude to us, and they did not help us. I'll leave the code here, and the if with the error is marked with a comment ahead (it's in the last if, to be more exact). Basically, this code allows me to enter the name of the city, saying whether it exists in the file or not, and then presented the meteorological information corresponding to that city, but the program stops running after confirming that the city exists. Every help is welcome. And thank you!

int search_date(){
char input[100];
char id_input[100];
char id_cidade[100];
char cidade[100];
char concelho[100];
char distrito[100];
int i=1;

printf("Introduza localidade a pesquisar: ");    
scanf("%s", input);    

FILE* stream = fopen("cidades.csv", "r");
if (stream != NULL) {

    char line[1024];
    while (fgets(line, 1024, stream))
    {
        char *tmp = strdup(line);
        if (i > 0) {
            strcpy(id_cidade, strtok(tmp, ","));  
            strcpy(cidade, strtok(NULL, ","));  
            strcpy(concelho, strtok(NULL, ","));  
            strcpy(distrito, strtok(NULL, ","));  

            if (strcasecmp(cidade, input) == 0 || strcasecmp(concelho, input) == 0 || strcasecmp(distrito, input) == 0) {
                printf("%s - %s - %s - %s \n", id_cidade, cidade, concelho, distrito);
                printf("id_cidade: %s \n", id_cidade);
                strcpy(id_input, id_cidade);  
            }
            else {
            }   
        }
        i++;      
        free(tmp);
    }
} else {
    printf("INVALIDO");
}  

FILE* meteoFile = fopen("meteorologia.csv", "r");
if (meteoFile != NULL) {

    char line[1024];
    char id_meteo[100];    
    char id_meteo_cidade[100];    
    char temp_minima[100];    
    char temp_maxima[100];    
    char humidade[100];    
    char pressao[100];    
    char data[100];    
    int i=1;

    while (fgets(line, 1024, meteoFile))
    {
        char *tmp2 = strdup(line);

        if (i > 0) {
            strcpy(id_meteo, strtok(tmp2, ","));  
            strcpy(id_meteo_cidade, strtok(NULL, ","));  
            strcpy(temp_maxima, strtok(NULL, ","));  
            strcpy(temp_minima, strtok(NULL, ","));  
            strcpy(humidade, strtok(NULL, ","));  
            strcpy(pressao, strtok(NULL, ","));  
            strcpy(data, strtok(NULL, ","));

            printf("A obter os dados...\n");

            if (strcasecmp(id_meteo_cidade, id_cidade) == 0) //o erro está neste if
            {
                printf("Informacao meterologica para:%s em %s \nTemperatura Maxima: %s ºC\nTemperatura Minima: %s ºC\nHumidade: %s \nPressao: %s hPa\n\n\n", cidade, data, temp_maxima, temp_minima, humidade, pressao);
                // printf("encontrei \n");      
            }
            else
            {
                // printf("nao encontrei \n");      
            }   
        }
        i++;      
        free(tmp2);
    }
} else {
    printf("INVALIDO");
}
}
    
asked by anonymous 14.02.2018 / 16:21

0 answers