Hi, i'm just mixing file handling and struct types and pointers to learn, but i'm a little stuck on this.
I might be doing something awfull i can tell, sorry for that. Can anyone help? What's the best way to pass
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
struct persona {
char nombre[15];
char apellido[15];
unsigned edad;
};
int edadMedia(struct persona *);
int main()
{
char *filename = "C:\\Usuarios\\Administrador\\Escritorio\\C\\test.txt";
printf("Accediendo a %s:\n", filename);
FILE *file = fopen(filename, "r");
struct persona nombres[10];
int counter = 0, contador = 0;
char *linea;
linea = malloc(15 * sizeof(char));
while((fgets(linea, 15, file)) && *linea != EOF)
{
printf("Reading line %i -> %s", counter, linea);
if(linea[0] == '#') continue;
switch (counter)
{
case 0:
strcpy(nombres[contador].nombre, linea);
counter++;
break;
case 1:
strcpy(nombres[contador].apellido, linea);
counter++;
break;
case 2:
nombres[contador].edad = (unsigned int) atoi(linea);
counter = 0;
contador++;
break;
}
}
printf("\n\n\t[i] Detectados %i miembros!\n", contador);
printf("\t[i] Edad media %i aƱos", edadMedia(nombres));
return 0;
}
int edadMedia(struct persona * nombres) { //code still to go here }