I've been eating my head for several days to find an error in the program, because it does not read the data from the file I created. I've been programming for a month (It's in C language) because I'm quite new and everything seems very difficult. The exercise says the following:
We are asked to make a program to manage a file with the data of the runners and their times in a local cycling lap of 5 stages.
The file will contain the list of cyclists with their names and sorted by dorsal number. After each name the date goes (format day, month and year) and times, expressed in hours, minutes and seconds, for each of the stages. The abandonments in a determined stage, is indicated by null values of times in that stage. Make a modular program with functions for:
- (i) Initially enter the list of dorsais and names of the runners and null values of times in each stage,
- (ii) Read the file and save the data in a vector of structures (one for each corridor)
- (iii) Update the file data with the results of the last stage
- (iv) Calculate the number of dropouts including a certain stage, indicated by date,
- (v) Determine the leader at any time and return your name.
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
typedef struct {
int dorsal,h[5],min[5],seg[5],dia[5],mes[5],ano[5];
char nombre [30];
}etapas;
int main(int argc, char** argv) {
etapas corred [4];
int dec,i;
printf ("Desea introducir datos? \n 0--No 1---Si");
scanf("%i",&dec);
if (dec==1){
escribir (corred);
}
printf ("Ha elegido la opción leer el archivo,procedemos con la lectura ");
leer (corred);
for(i=0;i<4;i++) {
printf("%s %i",corred[i].nombre,corred[i].dorsal);
}
return (EXIT_SUCCESS);
}
void escribir ( etapas corredo [4]){
FILE *volta;
volta=fopen ("voltinha.txt","w");
int i,j;
for (i=0;i<4;i++){
printf("Introduce o nome do corredor %i :", i+1);
scanf("%s",corredo[i].nombre);
printf("Introduce un dorsal para o corredor %i : ",i+1);
scanf("%i", &corredo[i].dorsal);
fprintf(volta," %s %i \n", corredo[i].nombre,corredo[i].dorsal);
for (j=0;j<5;j++){
( corredo[i].h[j])=0;
( corredo[i].min[j])=0;
( corredo[i].seg[j])=0;
( corredo[i].ano[j])=0;
( corredo[i].mes[j])=0;
( corredo[i].dia[j])=0;
fprintf(volta,"%i %i %i %i %i %i \n",corredo[i].ano[j],corredo[i].mes[j],corredo[i].dia[j],corredo[i].h[j],corredo[i].min[j],corredo[i].seg[j]);
}
}
fclose(volta);
}
void leer (FILE *volta) {
etapas corredorr [4];
int i;
if ((volta=fopen("voltinha.txt","r"))==NULL){
printf("No se ha podido abrir el archivo");
exit(1);
}
while (feof(volta)==0){
fscanf(volta,"%s",corredorr[i].nombre);
fscanf (volta,"%i",&corredorr[i].dorsal);
i++;
for (int j=0;j<5;j++){
fscanf (volta,"%i",corredorr[i].ano[j]);
fscanf (volta,"%i",corredorr[i].mes[j]);
fscanf (volta,"%i",corredorr[i].dia[j]);
fscanf (volta,"%i",corredorr[i].h[j]);
fscanf (volta,"%i",corredorr[i].min[j]);
fscanf (volta,"%i",corredorr[i].seg[j]);
}
}
fclose(volta);
}
This is what I have been doing so far, he writes me the file but he does not read it to me.