Fich: Leer datos de alumnos

fichero leer datos de alumnos.html
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
	FILE *f;
	char direccion[] ="C:\\Users\\leeralumnos.txt";
	char opcion;
	
	struct alumnos{
		char nombre[20];
		char apellido[20];
		int edad;
		float nota;
	};
	struct alumnos alum;
	
	f = fopen(direccion,"a");
	
	do{
		// Primero solicita datos por pantalla
		printf("Introduzca informacion del alumno:\n");
		printf("Nombre: ");
		scanf("%s", alum.nombre); // Guardar en struct
		fflush(stdin);
		printf("Apellido: ");
		scanf("%s", alum.apellido); // Guardar en struct
		fflush(stdin);
		printf("Edad: ");
		scanf("%d", &alum.edad); // Guardar en struct
		fflush(stdin);
		printf("Nota: ");
		scanf("%f", &alum.nota); // Guardar en struct
		fflush(stdin);
		
		// Ahora escribir en archivo = FPRINTF
		fprintf(f,"%s - %s - %d - %f  \n", alum.nombre, alum.apellido, alum.edad,alum.nota);
		
		// Preguntar si quiere seguir ingresando datos
		printf("Quiere seguir ingresando mas datos? (y/n): ");
		scanf("%c",&opcion);
		fflush(stdin);
		
	}while(opcion == 'y' || opcion == 'Y');
	
	fclose(f);
	
	system("pause");
	return 0;
}

No hay comentarios:

Publicar un comentario