-1

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,

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
llinasenc
  • 11
  • 1
  • 1
    I would recommend you to pick a language, be it Spanish or English. This mixture doesn't seem a good practice. – arboreal84 May 17 '17 at 08:01
  • what is the problem? – Riccardo Bonafede May 17 '17 at 08:10
  • 1
    I would recommend reading from one file, whilst updating another, and then when finished, delete the original file and rename the new one. What you are doing is almost guaranteed to fail in bizarre ways, – Neil May 17 '17 at 08:13
  • How would you propose to replace say `9` in a text file with `10` by "inserting"? From where do you acquire another byte? – Weather Vane May 17 '17 at 08:14

1 Answers1

0

The best way to iterate through a file is to use the function getline(). Here is an example of its utilisation, taken from this post which I advise you read.

char const* const fileName = "best_teams.txt" ; // 
FILE* file = fopen(fileName, "r"); /* should check the result */
if (file != NULL) {

    char line[256];
    while (getline(line, sizeof(line), file)) { // Each iteration, a line will be stored in string `line`
           // Do what you want to do
   } // Exits when arrives at the end of the file
else puts("Error while opening file\n");

As suggested in the comments, you can then use fopen("best_teams.txt", "w") The "w" means write mode, which is described as follow in fopen documentation :

Creates an empty file for writing. If a file with the same name already exists, its content is erased and the file is considered as a new empty file.

Another solution is to open in read and write mode, and to only change the values you desire, but it might be more complicated.

Community
  • 1
  • 1
Badda
  • 1,329
  • 2
  • 15
  • 40
  • what is `getline()` and where is it in your snippet? –  May 17 '17 at 08:19
  • The problem is that, as is an exercisei should do, I have to follow the rules that have been given. And this implies using open function. – llinasenc May 17 '17 at 08:23
  • @FelixPalmen Oh, yeah, sorry. Fixed – Badda May 17 '17 at 08:25
  • @llinasenc So you are forced to use open read and write system functions even if every function manipulating files uses them indirectly ? – Badda May 17 '17 at 08:30