write a struct inna file

D

Dennis Schulz

hi all,

im a very beginner in c language and this is my try in writing /
appending a struct in a file.
unfortuately nothing is written into the file.
especially the part with the pointer Fischzeiger seems very strange to
me...
but fwrite accepts a pointer as first argument, i dont know what else
to do...

any help wounld be appreciated.

MfG Dennis


here is the code part:

#define dateiname "fischfile.dat"

// eigener Datentyp fis
typedef struct fis {
char besitzer[20];
char fischname[20];
int preis;
} Fischsatz;

int NeuerDatensatz(char besitzer[],char fisch[]) {
FILE *fischfile;
// pruefe ob datei schon existiert
if (DateiExistiert(dateiname)) {
// oeffne Datei zum anhaengen
fischfile =fopen(dateiname, "a");
} else {
// oeffne Datei zum schreiben
fischfile =fopen(dateiname, "w");
}

// lege neuen Fisch an
Fischsatz neufisch;
strcpy(neufisch.besitzer, besitzer);
strcpy(neufisch.fischname, fisch);
neufisch.preis = 17;

// fischzeiger auf neufisch
Fischsatz *fischzeiger;
fischzeiger = malloc(sizeof(struct fis));
fischzeiger = &neufisch;

// schreibe Fischstrukt in datei
fwrite(fischzeiger, sizeof (struct fis), 1, fischfile);

fclose(fischfile);
}
 
R

Robert Bachmann

Dennis said:
hi all,

im a very beginner in c language and this is my try in writing /
appending a struct in a file.
unfortuately nothing is written into the file.
especially the part with the pointer Fischzeiger seems very strange to
me...
but fwrite accepts a pointer as first argument, i dont know what else
to do...

any help wounld be appreciated.

MfG Dennis
You should not use "MfG" in english, but if
you like german you can also try de.comp.lang.c.

I just had a very short look on your code...
here is the code part:

#define dateiname "fischfile.dat"

// eigener Datentyp fis
typedef struct fis {
char besitzer[20];
char fischname[20];
int preis;
} Fischsatz;
int NeuerDatensatz(char besitzer[],char fisch[]) {
^^^
NeuerDatensatz shall return an integer, but
your code below fails to do so.
FILE *fischfile;
// pruefe ob datei schon existiert
if (DateiExistiert(dateiname)) {
// oeffne Datei zum anhaengen
fischfile =fopen(dateiname, "a");
I guess you want to write to this file in binary mode,
so use fischfile =fopen(dateiname, "ab");
} else {
// oeffne Datei zum schreiben
fischfile =fopen(dateiname, "w");

fischfile =fopen(dateiname, "wb");
You also should check if fischfile is != NULL.
// lege neuen Fisch an
Fischsatz neufisch;
strcpy(neufisch.besitzer, besitzer);
strcpy(neufisch.fischname, fisch);
You should check if besitzer and fischname do
not 20 characters (including the terminating '\0').
neufisch.preis = 17;

// fischzeiger auf neufisch
Fischsatz *fischzeiger;
fischzeiger = malloc(sizeof(struct fis));
fischzeiger = &neufisch;
Forget this 4 lines.
Use
fwrite( &neufisch, sizeof (neufisch), 1, fischfile);
instead of
fwrite(fischzeiger, sizeof (struct fis), 1, fischfile);

fclose(fischfile);
}

Best regards,
Robert Bachmann
 
M

Mark McIntyre

You should not use "MfG" in english,

I doubt that any of the non-german speakers will be offended by "mit
freundlichen grussen". Most of them probably think its his initials.
 
A

August Derleth

I doubt that any of the non-german speakers will be offended by "mit
freundlichen grussen". Most of them probably think its his initials.

And those who have access to the Google translation service know that
"mit freundlichen grueßen" simply means "Yours sincerely".

Hardly offensive content.
 
I

Irrwahn Grausewitz

August Derleth said:
And those who have access to the Google translation service know that
"mit freundlichen grueßen" simply means "Yours sincerely".

Hardly offensive content.

It /could/ as well mean "Mit feindlichem Grinsen"[1] - that's what I
think of when I use it in a letter to someone I really don't like.
;-)

[1] literal translation: "With a hostile grin"

Regards
 
R

Robert Bachmann

Mark said:
I doubt that any of the non-german speakers will be offended by "mit
freundlichen grussen". Most of them probably think its his initials.

I didn't mean that using "MfG" or "Mit freundlichen Grüßen" in english
is impolite. It just seams funny to me. ;-)
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top