Buenas, no estaria detectando por que este programa me genera un ciclo infinito, desde ya muchas gracias.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CODDEPO 6
#define NOMDEPO 15
typedef struct{
char coddepor[CODDEPO];
unsigned int tiempo;
}competencia;
typedef struct{
char coddepor[CODDEPO];
char nomdepor[NOMDEPO];
unsigned int tiempo;
}record;
void cargador();
void cargatabla(record tablarec[],int *n);
void procesa();
int buscaindice(record tablarec[],int n,char depor_act[]);
int main()
{
record tablarec[20];
int n;
cargador();
cargatabla(tablarec,&n);
procesa(tablarec,n);
return 0;
}
void cargador(){
competencia compe;
record rec;
FILE *competiciontxt, *competicionbin;
FILE *recordstxt,*recordsbin;
competiciontxt
= fopen("competicion.txt","r"); competicionbin
= fopen("competicion.dat","wb"); recordstxt
= fopen("records.txt","r"); recordsbin
= fopen("records.dat","wb");
if(competiciontxt == NULL){
printf("El archivo competicion no existe."); }else{
while(fscanf(competiciontxt
,"%s %d",compe.
coddepor,&compe.
tiempo) == 2){ fwrite(&compe
,sizeof(competencia
),1,competicionbin
); }
}
if(recordstxt == NULL){
printf("El archivo records no existe."); }else{
while(fscanf(recordstxt
,"%s %s %d",rec.
coddepor,rec.
nomdepor,&rec.
tiempo) == 3){ fwrite(&rec
,sizeof(record
),1,recordsbin
);
}
}
}
void cargatabla(record tablarec[],int *n){
FILE *arch;
arch
= fopen ("records.dat","rb"); (*n) = 0;
if (arch == NULL){
printf("Ocurrio un error con la lectura del archivo records.dat"); }else{
fread(&tablarec
[*n
],sizeof(record
),1,arch
); (*n)++;
fread(&tablarec
[*n
],sizeof(record
),1,arch
); }
}
}
void procesa(record tablarec[],int n){
competencia compe;
int cant_comp,cant_sup,sum_t,indice;
float t_prom;
char depor_act[CODDEPO];
FILE *competicion;
competicion
= fopen("competicion.dat","rb");
if (competicion == NULL){
printf("Ocurrio un error con la lectura del archivo competicion."); }else{
printf("DEPORTE \t CANT.COMPETIDORES \t CANT. SUPERO EL RECORD \t TIEMPO PROMEDIO \n"); fread(&compe
,sizeof(competencia
),1,competicion
); while(!feof(competicion
)){ cant_comp=0;
cant_sup=0;
t_prom=0;
sum_t=0;
strcpy(depor_act
,compe.
coddepor); indice = buscaindice(tablarec,n,depor_act);
while(!feof(competicion
) && !strcmp(depor_act
,compe.
coddepor) ){ if (compe.tiempo > tablarec[indice].tiempo){
cant_sup++;
}
sum_t+=compe.tiempo;
cant_comp++;
fread(&compe
,sizeof(competencia
),1,competicion
); }
t_prom=(sum_t/(float)cant_comp);
printf("%s \t %d \t %d \t %5.2f \n",depor_act
,cant_comp
,cant_sup
,t_prom
); }
}
}
int buscaindice(record tablarec[],int n,char depor_act[]){
int i,indice;
indice=0;
for (i=0;i<n;i++){
if(strcpy(depor_act
,tablarec
[i
].
coddepor)){ indice=i;
}
}
return indice;
}
ARCHIVO TXT COMPETICION:
bicic 5
bicic 7
bicic 6
bicic 5
carre 10
carre 15
carre 9
carre 16
carre 12
natac 10
natac 16
natac 20
natac 5
patin 4
patin 7
patin 6
patin 8
ARCHIVO TXT RECORDS
carre Carrera 10
patin Patin 4
bicic Bicicleta 7
natac Natacion 18
MOD: El código debe ir entre etiquetas de Código GeSHi