I have to write an app on C that opens a file, reads what is inside of it, stores that information on a struct and prints all the information. I did it creating "empleados.txt" and placing it on the folder where is main.c. This is the content of the .txt:
Juan Perez Rodriguez 11111111A 1200 Pepa Bueno Ruibal 22222222B 2300 It is not printing anything, and I don't know why.
#include <stdio.h> #include <stdlib.h> struct Empleado { char nombre[15]; char apellido_uno[30]; char apellido_dos[30]; char DNI[9]; unsigned int sueldo; }; int main() { int i=0, j; struct Empleado num[50]; FILE *idf; idf=fopen("empleados.txt", "rt"); if (idf==NULL){ printf("Error abriendo fichero ..."); exit(1); } else { do{ fgets(idf,"%s",&num[i].nombre); fgets(idf,"%s",&num[i].apellido_uno); fgets(idf,"%s",&num[i].apellido_dos); fgets(idf,"%s",&num[i].DNI); fgets(idf,"%d",&num[i].sueldo); i++; }while(feof(idf)==0); } fclose (idf); for(j=0;j<i;j++){ printf("Nombre: %s",num[i].nombre); printf("Nombre: %s",num[i].apellido_uno); printf("Nombre: %s",num[i].apellido_dos); printf("Nombre: %s",num[i].DNI); printf("Nombre: %o",num[i].sueldo); } return 0; }
rt?fscanfinstead offgets, andchar DNI[9];-->char DNI[10];jfor index. alsowhile(feof(idf)==0);is wrong.