Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > iteration through a file of structs

Reply
Thread Tools

iteration through a file of structs

 
 
Dennis Schulz
Guest
Posts: n/a
 
      05-08-2004
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;
}
}
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      05-08-2004

"Dennis Schulz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      05-08-2004
Dennis Schulz <(E-Mail Removed)> 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 ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Iteration through File.file? misses entries for whichFile.file?(entry) == true Kyle Barbour Ruby 10 08-02-2010 08:55 PM
Struts - Problem with nested iteration or double iteration Rudi Java 5 10-01-2008 03:30 AM
Array of structs instead of an array with pointers to structs? Paminu C Programming 5 10-11-2005 07:18 PM
const structs in other structs Chris Hauxwell C Programming 6 04-27-2004 07:03 PM
structs with fields that are structs Patricia Van Hise C Programming 5 04-05-2004 01:37 AM



Advertisments