473,408 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

iteration through a file of structs

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;
}
}
Nov 14 '05 #1
2 1151

"Dennis Schulz" <d.********@gmx.net> wrote in message
news:26**************************@posting.google.c 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
Nov 14 '05 #2
Dennis Schulz <d.********@gmx.net> 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 ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ...
59
by: Raymond Hettinger | last post by:
Please comment on the new PEP for reverse iteration methods. Basically, the idea looks like this: for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0 <do something with i> The...
0
by: Danny Anderson | last post by:
Hola, C++ folk! I want to have a do...while loop that works with a different file each iteration, prompting the user for the file name to open and write. The problem I am having is that the...
5
by: Angel | last post by:
Is there some way to add a C-header file into a C# project? The problem is that this .h file contains several complex structures (over 20) that are used by some unmanaged code. These functions...
2
by: Jack Ukleja | last post by:
Hi guys, I'm trying to figure out a way to do reflection on structs which are in a legacy c++ header file. Basically I want to write a C++/CLI program that lets me edit the binary data in the...
2
by: Eric | last post by:
Hi, I need to send data from a file into a structure. In Vb6 this was done like so. public Type Struct1 v1 as string v2 as string end type
4
by: engggirl3000 | last post by:
I was told to write a function that will read data, in a user defined text file, into a vector of structs. I am not sure where to start with this. Could some one tell me what is a vector of...
3
by: Pietro Cerutti | last post by:
Hi Group, suppose test1.c, test2.c and test.h /*** BEGIN TEST.H ***/ #ifndef _TEST_H #define _TEST_H typedef struct {
8
by: =?Utf-8?B?YmJn?= | last post by:
I am getting this error when I tried to modify one field inside foreach loop. public struct myStruct { public int a; public bool b; //... } private List<myStructMyStruct = new...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.