iteration through a file of structs

Discussion in 'C Programming' started by Dennis Schulz, May 8, 2004.

  1. Hi all,

    i am iterating through the whole file looking for besitzer.
    When there is a match, i want the current record to be modified (add
    zuschlag) and saved again at the same position in the file.
    Here is the problem:
    the fwrite in my coding overwrites the next data record
    instead of replacing the current one. How can I jump one record back?
    Do I have remember the index and iterate from the beginning again, or
    is there a more elegant method?


    Thanks, Dennnis


    int Zuschlag(char besitzer[],int zuschlag) {
    printf("Zuschlag fuer %s\n\n", besitzer);
    FILE *fischfile;
    int fisch_geschrieben;
    // enthält die anzahl der von fread gelesenen saetze
    int satz_gelesen;
    // öffne datei als binaerstream zum lesen und schreiben
    fischfile=fopen(DATEINAME, "r+b");
    Fischsatz akt_fisch;
    do {
    satz_gelesen = fread(&akt_fisch, sizeof akt_fisch, 1,
    fischfile);
    // wenn erfolgreich gelesen...
    if (satz_gelesen != 0) {
    if (strcmp(akt_fisch.besitzer, besitzer)==0) {
    akt_fisch.preis = akt_fisch.preis + zuschlag;


    //*********************************************************
    // Here is the problem: the fwrite overwrites the next data
    // record instead of replacing the current one. How can I jump one
    // record back? Do I have remember the index and
    iterate from the
    // beginning again, or is the a more elegant method?
    //*********************************************************
    fisch_geschrieben = fwrite(&akt_fisch, sizeof
    akt_fisch, 1, fischfile);
    }
    }
    } while (satz_gelesen == 1); //bis kein satz mehr gelesen wurde

    if (fclose(fischfile)==EOF) {
    fprintf(stderr, "Fehler beim Schliessen");
    return -3;
    }

    if (fisch_geschrieben!=1) {
    fprintf(stderr, "Fehler beim Schreiben");
    return -2;
    }

    if (ferror(fischfile)) {
    /* An error occurred on the stream */
    fprintf(stderr, "Fehler beim Streaming");
    return -3;
    } else {
    /* end-of-file was encountered on the stream */
    printf("%s","END OF FILE");
    return 0;
    }
    }
     
    Dennis Schulz, May 8, 2004
    #1
    1. Advertising

  2. Dennis Schulz

    Mike Wahler Guest

    "Dennis Schulz" <> wrote in message
    news:...
    > Hi all,
    >
    > i am iterating through the whole file looking for besitzer.
    > When there is a match, i want the current record to be modified (add
    > zuschlag) and saved again at the same position in the file.
    > Here is the problem:
    > the fwrite in my coding overwrites the next data record
    > instead of replacing the current one. How can I jump one record back?


    fseek()

    -Mike
     
    Mike Wahler, May 8, 2004
    #2
    1. Advertising

  3. Dennis Schulz

    -berlin.de Guest

    Dennis Schulz <> wrote:
    > i am iterating through the whole file looking for besitzer.
    > When there is a match, i want the current record to be modified (add
    > zuschlag) and saved again at the same position in the file.
    > Here is the problem:
    > the fwrite in my coding overwrites the next data record
    > instead of replacing the current one. How can I jump one record back?
    > Do I have remember the index and iterate from the beginning again, or
    > is there a more elegant method?


    Yes, there's the fseek() function. Call it with the file pointer
    as the first, the number of bytes you want to skip back (in this
    case that's a negative number, use a positive one to skip forward)
    as the second and SEEK_CUR as the third argument.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, May 8, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    666
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    586
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    660
    Eric Sosman
    Oct 11, 2005
  4. Rudi
    Replies:
    5
    Views:
    5,303
  5. Kyle Barbour
    Replies:
    10
    Views:
    607
    Marvin Gülker
    Aug 2, 2010
Loading...

Share This Page