I have problems with an exercise in C. I need to compare a string of text stored in a file.
File content (client.txt):
Jose Gomez Santos
Maria Sanz Trementina
Jesus Gomez Soler
First name, last name, second last name
I have to compare the first surname of all with one that the user enters.
My code is as follows:
#include <stdio.h>
#include <string.h>
#define MAX 3 //Quantitat de personas al arxiu 'clients.c'
int main() {
FILE *file;
char linea[1024];
char *persona1, *persona2, *persona3;
char *frase1;
char *frase2;
char *frase3;
char *persona;
int i;
file = fopen("clients.txt", "w+");
persona1 = "Jose \t Gomez\t Santos\n";
persona2 = "Maria \t Sanz\t Trementina\n";
persona3 = "Jesus \t Gomez\t Soler\n";
fwrite(persona1, 1, strlen(persona1), file);
fwrite(persona2, 1, strlen(persona2), file);
fwrite(persona3, 1, strlen(persona3), file);
rewind(file);
printf("Introduix el primer cognom d'una de les persones\n");
scanf("%s", persona );
printf("------------------------------\n");
printf("PERSONES EN EL FITXER\n");
for (i=0; i<MAX; i++)
{
fgets(linea, 1024, file);
puts(linea);
frase1 = strtok(linea, "\t");
frase2 = strtok(NULL, "\t");
frase3 = strtok(NULL, "\t");
if( strcmp(persona , frase2) == 0 )
{
printf("Aquest cognom existeix !!\n");
printf("%s\t%s\t%s\n", frase1, frase2, frase3);
} else {
printf("Aquest cognom no existeix\n");
}
printf("\n-------------------------------------\n");
}
fclose(file);
}
The error that my code presents is that it does not allow me to compare in the "strcmp" the variable with the surname that the user "persona" inserts, with the last name of the variable "frase2" that contains the file called "clients. txt ".
The error it gives me is "core generated" and I do not know what the error is concretely. Thank you very much for the answer