Doubt in reading files to a vector of structures

0

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.

    
asked by Flashbach 09.11.2017 в 18:52
source

1 answer

0

I see two errors that are causing you to read on undefined memory values. One is that in the function void leer (FILE *volta) you do not have initialized the value of the counter i in 0 .

The other mistake I see is that in instructions like this:

fscanf (volta,"%i",corredorr[i].ano[j]);

you should replace it with the following:

fscanf (volta,"%i",&(corredorr[i].ano[j]));

What happens is the following, the function:

int fscanf(FILE *stream, const char *format, ...)

in the additional arguments (...) wait for the addresses of the elements in which to place the values obtained by the stream. When having as argument the following:

corredorr[i].ano[j]

You are passing a value and not a address . Since the value stored in your program for said variable is 0 , you are telling your program to access that memory location.

Probably a notation with pointers of what is happening will clarify what has been said. What you have originally is the following:

corredorr[i].ano[j]corredorr[i].ano[j]

this will return a value. Now, the following is the same but in pointer notation:

*(corredorr[i].ano + j)

the indirection makes the fact that the expression returns a value more noticeable. Now compare it with the following expressions that are equivalent:

&(corredorr[i].ano[j]);
&(*(corredorr[i].ano + j))

The & operator makes the fact that the expression returns an address more readable.

    
answered by 12.11.2017 в 08:11