0
Hello! I’m new to programming, and for a college job, I have to create a project where I have to use .csv. files.The program itself compiles, but I know I have an error in an if function, and I can’t identify it. I looked for examples, but I couldn’t find anything. Me and a friend also came to ask similar questions on the site in English, but were quite rude to us, and did not help us. I will leave the code here, and if with the error is marked with a comment in front (it is 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 present the meteorological information corresponding to that city, but the program stops running after confirming that the city exists. Any help is welcome. And thanks in advance!
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");
}
}
the function
strcasecmp
compares two strings, and in the case the parameters seem to be correct, since both variables are declared aschar
, but that function is in a library, I think strings. h, you used a include in your program? something like#include <strings.h>
– Ricardo Pontual
Yes, I used the <stdio. h>, <stdlib. h> and <string. h>
– Jessijow Black
Can you put an example of these csv files ? can be one of the cities.csv and its correspondent in meteorology.csv
– Zapello
The.csv file has 152 rows and 4 columns and this format: id_city, city, county, district (Ex.: 98,Porto,Porto,Porto) The.csv file has 152 rows and 7 columns and this format: id_meteo_city,id_city,temp_max,temp_min,humidity, pressure, date (Ex.: 98,98,9.5,-0.3,76,1024,2018-02-12)
– Jessijow Black
Apparently csv values are okay. Looking at the code I saw 3 problems: the first one is that your first i is initialized with 0 and then checked "if (i > 0)" then it will never read the first line. The second problem is that even if the input city is not found it is trying to find meteorological data, it might be better to put a "No Encoding City" message and exit the program. The third problem is the printf inside the if you say is the problem, it is with the wrong parameters has six "%s" and 7 variables, has a "%" lost.
– Zapello
As a suggestion I would put a printf ante do if you think you are the problem, to be able to see what the value is being compared, if it is not a junk in memory or some more character, for example a space.
– Zapello
@Psico: Thanks for the help, I’ll change and see if the program works. That lost percent is purposely, because I’m referring to the humidity percentage... it’s just to show up the unit
– Jessijow Black
@Psico: I already changed the code according to the tips you gave me. But still the same happens. I compile the program, enter the city, display the search result of the.csv cities file, and then the program simply stops running... Then the error can not be in if, but in something before, since even the printf you advised me to insert appears...
– Jessijow Black