I had a question about how to read a text file in which the data come one in each line and enter them in the variables that I want.
Let me explain:
#include <stdio.h>
int main()
{
char fichero[100], nombre[100], apellido[100];
int edad;
FILE *f;
printf("\n\nIntroduce el fichero desde donde quieras importar los datos: ");
fflush(stdin);
gets(fichero); //Introduzco Manualmente el nombre del fichero
f = fopen(fichero, "r"); //Abro el fichero en modo lectura
**//****ES UN ARCHIVO .TXT******
if(f == NULL) //Compruebo que se ha abierto correctamente
{
printf("Error.No se ha podido abrir el fichero");
exit(1);
}
fgets(nombre, 100, f); //Utilizo la funcion fgets para coger cadena de
fscanf(f, "%d", &edad); //caracteres y fscanf para tomar numeros y los
fgets(apellido, 100, f); //guardo en las variables anteriormente creadas
printf("\n\nDATOS"); //Muestro esas variables por pantalla
printf("\n=======");
printf("\n\nNombre: %s",nombre);
printf("\nEdad: %i",edad);
printf("\nApellido: %s",apellido);
printf("\n\n\nPulse <intro> para finalizar...");
system("pause");
return 0;
}
As you can see, the only thing I'm trying to do is open the file and copy the data, but I'm not able to save it. The content of the .txt file from which you are taking the data is as follows:
Luis Miguel
28
Perez Hernandez
Any idea how I can do?