Thread: modifying information from a binary file

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    1

    modifying information from a binary file

    this code is not working and i have no idea why
    Code:
    void modificare_cheie_unica(char *nume)
    {
      FILE *g;
      FLOARE f;
      fopen_s(&g, nume, "rb+");
      if (!g)
        printf("Eroare");
      else {
        int cod = 0;
        printf("Cod floare:");
        scanf_s("%d", &cod);
        while (fread(&f, sizeof(f), 1, g) == 1) {
          if (cod == f.codf) {
    
            printf("Pretul vechi este: %f\n", f.pret);
            printf("Noul pret este:");
            scanf_s("%f", &f.pret);
            fseek(g, cod * sizeof(f), 0);
            fwrite(&f, sizeof(f), 1, g);
    
          } else
            printf("Nu exista\n");
    
          printf("Cod floare:");
          scanf_s("%d", &f.codf);
    
        }
    
        fclose(g);
      }
    
    }
    Last edited by Salem; 03-18-2019 at 11:26 AM. Reason: Removed crayola

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Since you haven't posted a runnable program I can't actually try it, but I do see some oddities.

    The oddest thing is that you seem to be mixing sequential access with random access. You are trying to find the cod with a sequential search (the while loop), but then you fseek to cod*sizeof(f), which is random access (where you jump directly to the correct position). Which is it? Are the cod numbers the 0-based offsets of the records? If so, then you should do something like:
    Code:
    // ...get cod from user, then...
     
    if (fseek(g, cod * sizeof(f), SEEK_SET) != 0)
        printf("Record not found\n");
    else if (fread(&f, sizeof(f), 1, g) != 1)
        printf("Record could not be read\n");
    else {
     
        // ...get the new price from the user, then...
     
        fseek(g, cod * sizeof(f), SEEK_SET);
        fwrite(&f, sizeof(f), 1, g);
    }
    If you mean it to be sequential access (cod is not the record offset), then you need to get rid of the else statement that prints "Nu exista". You don't know if it doesn't exist until after the loop is done.

    And when you do find it, the fseek needs to be something like the following in order to move back one record:
    Code:
        fseek(g, -sizeof(f), SEEK_CUR);
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modifying portion of binary files?
    By SaintDaveUK in forum C Programming
    Replies: 1
    Last Post: 08-14-2012, 03:48 AM
  2. Lopping through binary file to read information
    By squinchy in forum C Programming
    Replies: 4
    Last Post: 01-25-2012, 12:39 PM
  3. Modifying a file.
    By RoRo in forum C Programming
    Replies: 2
    Last Post: 11-24-2010, 10:00 PM
  4. Binary was not built with debug information.
    By studentffm in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2010, 12:13 PM
  5. Binary information
    By Scarvenger in forum Windows Programming
    Replies: 1
    Last Post: 09-05-2006, 07:49 PM

Tags for this Thread