AYUDA FICHEROS TXT
Publicado por quikiss (6 intervenciones) el 01/06/2006 19:30:33
Hola a todos..
He escrito el siguiente codigo la función DATOS guarda los datos en un fichero de texto, pero no consigo que la funcion MEJORNOTA me lee los datos del fichero, que estoy haciendo mal? Gracias..
void Datos(FILE *flFichero)
{
char cContinuar;
struct tRegistro Registro;
flFichero=fopen("alumnos.txt","a");
if(flFichero == NULL)
{
printf("Nose puede crear el fichero");
getch();
}
do{
clrscr();
fflush(stdin);
printf("\nTeclee el nombre:");
scanf("\n%s",&Registro.sNombre);
do{
printf("\nTeclee el sexo(V/M):");
scanf("%c",&Registro.cSexo);
}while(!(Registro.cSexo=='V')&& !(Registro.cSexo=='M'));
printf("\nTeclee la nota del primer trimestre: ");
scanf("\n%f",&Registro.fNota1);
printf("\nTeclee la nota del segundo trimestre:");
scanf("\n%f",&Registro.fNota2);
printf("\nTeclee la nota del tercer trimestre:");
scanf("\n%f",&Registro.fNota3);
Registro.fNotaMedia=(Registro.fNota1+Registro.fNota2+Registro.fNota3)/3;
fprintf(flFichero,"%s\t%c\t%.2f\t%.2f\t%.2f\t%.2f\n",Registro.sNombre,Registro.cSexo,Registro.fNota1,Registro.fNota2,Registro.fNota3,Registro.fNotaMedia);
printf("quieres continuar (S/N)?");
scanf("\n%c",&cContinuar);
}while(toupper(cContinuar)=='S');
fclose(flFichero);
}
void MejorNota(FILE *flFichero)
{
tRegistro Registro;
int i;
float fMejorNota=0;
char sAlumnoMejor[30];
if((flFichero=fopen("alumnos.txt","r"))==NULL)
{
printf("Error:No He podido abrir el fichero\n");
getch();
}
fscanf(flFichero,"%s\t%c\t%.2f\t%.2f\t%.2f\t%.2f\n",Registro.sNombre,Registro.cSexo,Registro.fNota1,Registro.fNota2,Registro.fNota3,Registro.fNotaMedia);
printf("\nNombre: ",Registro.sNombre);
printf("\nNota Media: ",Registro.fNotaMedia);
while(!feof(flFichero))
{
fscanf(flFichero,"\n%s\t%c\t%f\t%f\t%f\t%f",&Registro.sNombre,&Registro.cSexo,&Registro.fNota1,&Registro.fNota2,&Registro.fNota3,&Registro.fNotaMedia);
printf("\nNombre: ",Registro.sNombre);
printf("\nNota Media: ",Registro.fNotaMedia);
if(Registro.fNotaMedia>fMejorNota)
fMejorNota=Registro.fNotaMedia;
strcpy(sAlumnoMejor,Registro.sNombre);
}
fclose(flFichero);
printf("\nEl alumno que mejor nota media ha obtenido es %s ",sAlumnoMejor);
getch();
}
He escrito el siguiente codigo la función DATOS guarda los datos en un fichero de texto, pero no consigo que la funcion MEJORNOTA me lee los datos del fichero, que estoy haciendo mal? Gracias..
void Datos(FILE *flFichero)
{
char cContinuar;
struct tRegistro Registro;
flFichero=fopen("alumnos.txt","a");
if(flFichero == NULL)
{
printf("Nose puede crear el fichero");
getch();
}
do{
clrscr();
fflush(stdin);
printf("\nTeclee el nombre:");
scanf("\n%s",&Registro.sNombre);
do{
printf("\nTeclee el sexo(V/M):");
scanf("%c",&Registro.cSexo);
}while(!(Registro.cSexo=='V')&& !(Registro.cSexo=='M'));
printf("\nTeclee la nota del primer trimestre: ");
scanf("\n%f",&Registro.fNota1);
printf("\nTeclee la nota del segundo trimestre:");
scanf("\n%f",&Registro.fNota2);
printf("\nTeclee la nota del tercer trimestre:");
scanf("\n%f",&Registro.fNota3);
Registro.fNotaMedia=(Registro.fNota1+Registro.fNota2+Registro.fNota3)/3;
fprintf(flFichero,"%s\t%c\t%.2f\t%.2f\t%.2f\t%.2f\n",Registro.sNombre,Registro.cSexo,Registro.fNota1,Registro.fNota2,Registro.fNota3,Registro.fNotaMedia);
printf("quieres continuar (S/N)?");
scanf("\n%c",&cContinuar);
}while(toupper(cContinuar)=='S');
fclose(flFichero);
}
void MejorNota(FILE *flFichero)
{
tRegistro Registro;
int i;
float fMejorNota=0;
char sAlumnoMejor[30];
if((flFichero=fopen("alumnos.txt","r"))==NULL)
{
printf("Error:No He podido abrir el fichero\n");
getch();
}
fscanf(flFichero,"%s\t%c\t%.2f\t%.2f\t%.2f\t%.2f\n",Registro.sNombre,Registro.cSexo,Registro.fNota1,Registro.fNota2,Registro.fNota3,Registro.fNotaMedia);
printf("\nNombre: ",Registro.sNombre);
printf("\nNota Media: ",Registro.fNotaMedia);
while(!feof(flFichero))
{
fscanf(flFichero,"\n%s\t%c\t%f\t%f\t%f\t%f",&Registro.sNombre,&Registro.cSexo,&Registro.fNota1,&Registro.fNota2,&Registro.fNota3,&Registro.fNotaMedia);
printf("\nNombre: ",Registro.sNombre);
printf("\nNota Media: ",Registro.fNotaMedia);
if(Registro.fNotaMedia>fMejorNota)
fMejorNota=Registro.fNotaMedia;
strcpy(sAlumnoMejor,Registro.sNombre);
}
fclose(flFichero);
printf("\nEl alumno que mejor nota media ha obtenido es %s ",sAlumnoMejor);
getch();
}
Valora esta pregunta


0