I have a code where the user can change/add information to a array struct and parse it to a csv file. Since I'm really new to programming I truly have basic knowledge and no matter how much I research.
So this code is meant to do the following: It'll ask the user for a city and if it finds the line with said city the user could add information to a struct regarding that line and then save it to the csv file. The code shows no error however it stops running after I input a city name.
Here is the code:
Code:
struct wea_array{ //int id_cidade[100];
char cidade[100];
//char concelho[100];
//char distrito[100];
int temperatura[100];
int temp_max[100];
int temp_min[100];
int press[100];
int humi[100];
};
const char* getfield(char* line, int num)
{
const char* tok;
for (tok = strtok(line, ",");
tok && *tok;
tok = strtok(NULL, ","))
{
if (!--num)
return tok;
}
return NULL;
}
void structwea(){
struct wea_array* wea = malloc(sizeof(struct wea_array));
int i;
for (i=0;i<152;i++){
printf("Introduza o valor da temperatura:");
scanf("%s",wea[i].temperatura);
printf("Introduza o valor da temperatura maxima:");
scanf("%s",wea[i].temp_max);
printf("Introduza o valor da temperatura minima:");
scanf("%s",wea[i].temp_min) ;
printf("Introduza o valor da pressap:");
scanf("%s",wea[i].press);
printf("Introduza o valor da humidade:");
scanf("%s",wea[i].humi);
}
}
void edit(){
char input[100];
char id_input[100];
char id_city[100];
char cidade[100];
char concelho[100];
char distrito[100];
int i=0;
FILE* stream;
if (stream != NULL) {
printf("Introduce city to edit information:");
scanf ("%[^\n]%*c", input);
char line[1024];
while (fgets(line, 1024, stream))
{
char *tmp = strdup(line);
if (i > 0) {
strcpy(id_cidade, strtok(tmp, ",\n"));
strcpy(cidade, strtok(NULL, ",\n"));
strcpy(concelho, strtok(NULL, ",\n"));
strcpy(distrito, strtok(NULL, ",\n"));
//printf("%s - %s - %s - %s \n", id_cidade, cidade, concelho, distrito);
if (strcmp(cidade,input)==0 || strcmp(concelho,input)==0 || strcmp(distrito,input)==0){
structwea();
strcpy(id_input, id_cidade);
}
}
i++;
free(tmp);
}
}
else {
printf("INVALIDO");
}
}
int main(){
edit();
}
Any help is appreciated, thanks in advance!