I have a problem reading a file, it does not show me the correct age.
And this the code:
#include <stdio.h>
#include <string.h>
char codigo[3];
char nombre[100];
char edad[2];
void setInfoAlumno(char linea[]){
memset(codigo, 0, 3); //Si hay algun valor lo borro
memset(nombre, 0, 100); //Si hay algun valor lo borro
memset(edad, 0, 2); //Si hay algun valor lo borro
//Obtengo el codigo del alumno
for(int i=0; i<=2;i++){
codigo[i] = linea[i];
};
//Obtengo el nombre del alumno
int limite=0;
int j=0;
for(int i=4; i<=100; i++,j++){
if(linea[i] != '-') nombre[j] = linea[i];
else limite = i;
}
//Obtengo la edad del alumno
j=0;
for(int i=limite+1; i<=100;i++,j++){
edad[j] = linea[i];
}
}
int main(){
FILE *fAlumno = fopen("alumno.txt", "r");
char linea[100];
if(fAlumno != NULL){
while(feof(fAlumno)==0){
fgets(linea, 100, fAlumno);
setInfoAlumno(linea);
printf("Codigo: %s\n", codigo);
printf("Nombre: %s\n", nombre);
printf("Edad: %s\n", edad);
}
}else{
printf("Error al abrir el archivo alumno.txt");
}
}
As can be seen in the output of the program, the age of the two last ones is the same, while in the file they are not. What can be the error.