I need to know how could I read a file with values in different lines, and compare it with values I have in memory, to get wich line has a better score, and if values in memory are best, insert those values into the file (the file is open as read and as write at the same time, as this part of code could be run from several fork() at the same time):
if (PuntuacioEquip(jugadors)>MaxPuntuacio && CostEquip(jugadors)<PresupostFitxatges)
{
//Compare with file
int fd, n;
TBestEquip info;
fd = open("best_teams.txt", O_RDONLY);
if (fd<0) panic("open");
while ((n=read(fd, &info, sizeof(info))) == sizeof(info)) {
//printf(cad,"equip %lld, puntuacio %d\n", info.Equip, info.Puntuacio);
//write(1,cad,strlen(cad));
if (info.Puntuacio>PuntuacioEquip(jugadors))
{
fd = open("best_teams.txt", O_WRDONLY|O_TRUNC|O_CREAT,0600);
if (fd<0) panic("open");
sprintf(cad,"%s Cost: %d Points: %d. %s\n", CostEquip(jugadors), PuntuacioEquip(jugadors));
write(fd,cad,strlen(cad));
}
}
// We have a new partial optimal team.
MaxPuntuacio=PuntuacioEquip(jugadors);
memcpy(MillorEquip,&jugadors,sizeof(TJugadorsEquip));
sprintf(cad,"%s Cost: %d Points: %d. %s\n", color_green, CostEquip(jugadors), PuntuacioEquip(jugadors), end_color);
write(1,cad,strlen(cad));
}
Appreciate any help.
Regards,