Good day, I'm currently developing a program that has to be able to let the client introduce a player or a team and the data must go to a txt, name, id, etc.
Everything worked perfectly until I have had to use struct systems in the program, as it says that the array that I have used is incompatible with fgets.
{
int i=0;
FILE *equipos;
equipos=fopen("BaseDatos/equipos.txt", "rt");//Lee los equipos guardados en el fichero
if(equipos==NULL)
{
borraVentana(20,26,100,2);
rectangulo(19,25,99,1);
gotoxy( 26,26);
printf( "Error en el archivo");
fflush(stdin);
getchar();
return -1;
}
else
{
while(fscanf(equipos, "%d", Equipos[i])==1)//El bucle indica que el archivo se leera hasta que no haya ningún entero registrado en id_equipo
{
fgets(Equipos[i], 40-1, equipos);
i ;
}
fclose(equipos);
return i;//Devuelve el valor i siendo este el numero de equipos registrados
}
}
The line that doesn't works is fgets(Equipos[i], 40-1, equipos);
, I have tried to change it to fscanf but it doesn't works either, it keeps showing the same error:
error: incompatible type for argument 1 of 'fgets'.
Someone knows what I'm doing wrong?
Thanks.
Edit:
The composition of sEquipo is:
#define EQUIPO_H_INCLUDED
#define DIM_NOMBRE_EQUIPO 40
struct sEquipo
{
int id_equipo;
char nombre[DIM_NOMBRE_EQUIPO];
};
#endif // EQUIPO_H_INCLUDED```
It's supposed to take out from a txt the names of the teams so it can go to another function to use it.
CodePudding user response:
Your understanding of what structure and arrays are appear to be flawed.
A structure is a composition of any number of arbitrary types, with the individual members being accessed with the .
operator.
So, in order to read into the structure, you will need to treat each member individually.
while(fscanf(equipos, "%d", &Equipos[i].id_equipo)==1)
{
fgets(Equipos[i].nombre, 40-1, equipos);
i ;
}
fclose(equipos);
The fscanf will now take the address (mind the &
) of your member variable id_equipo
, whereas fgets gets the address of the character array nombre
(the &
is not necessary in this case, arrays decay to pointers automatically).
If Equipos
is not an array (hard to tell from your snippet), but much rather an array of pointers to the structure, the .
needs to replace with a ->
.
Alternatively, if you'd rather operate on a binary file and do not care about superfluous bytes in your file, you could just read the whole structure into memory by using fread
. Please note, that, if you do that, you must also write the file with the very same structure. If you later decide to expand your array or change it structure, all files you've created up to them will be incompatible.
But, just for completeness sake, this is your reading code, if you wanted to read the whole structure in one go:
while (fread(&Equipos[i], 1, sizeof(sEquipo), equipos) == sizeof(sEquipo))
i;
You will need to change your fopen
mode to rb
if you want to use that though.
CodePudding user response:
The first parameter of your fgets call is supposed to be a char*. You're currently providing a (unknown) array entry.
Also, your variable names are ambiguous. Don't give two variables the same name with different letter cases.
Try to call it with a char pointer instead. I'm not sure how your Array is defined.
char input[39];
fgets(input, 39, equipos);