iteration through a file of structs

D

Dennis Schulz

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;
}
}
 
M

Mike Wahler

Dennis Schulz said:
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
 
J

Jens.Toerring

Dennis Schulz said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top