473,396 Members | 1,827 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,396 software developers and data experts.

write a struct inna file

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);
}
Nov 14 '05 #1
5 1768
Dennis Schulz wrote:
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
Nov 14 '05 #2
On Thu, 06 May 2004 22:22:01 +0200, in comp.lang.c , Robert Bachmann
<Ro*************@rbdev.net> wrote:
Dennis Schulz wrote:

MfG Dennis

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.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #3
On Thu, 06 May 2004 22:51:51 +0100, Mark McIntyre wrote:
On Thu, 06 May 2004 22:22:01 +0200, in comp.lang.c , Robert Bachmann
<Ro*************@rbdev.net> wrote:
Dennis Schulz wrote:

MfG Dennis

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.


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

Hardly offensive content.

--
yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
To email me, rot13 and convert spelled-out numbers to numeric form.
"Makes hackers smile" makes hackers smile.

Nov 14 '05 #4
August Derleth <se*@sig.now> wrote:
Mark McIntyre wrote:
Robert Bachmann wrote:
Dennis Schulz wrote:

MfG Dennis
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.


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
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #5
Mark McIntyre wrote:
On Thu, 06 May 2004 22:22:01 +0200, in comp.lang.c , Robert Bachmann
<Ro*************@rbdev.net> wrote:

Dennis Schulz wrote:
MfG Dennis


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.


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

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

Similar topics

2
by: Thomas | last post by:
What's the quickest way to write and read 10.000 integer values ( or more ) to and from a file? Using struct somehow? The example in the docs shows how to handle to or three arguments, but is the...
3
by: Cesar Andres Roldan Garcia | last post by:
Hi I'm trying to write an hexadecimal file... I mean not a text plain... I have to convert a float decimal number in float hexadecimal one, and that's done. That number is the one I'm gonna...
27
by: Sune | last post by:
Hi! Pre-requisites: ------------------- 1) Consider I'm about to write a quite large program. Say 500 K lines. 2) Part of this code will consist of 50 structs with, say, no more than at most...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
8
by: Tony Houghton | last post by:
I'm writing a python program which reads input device events so it needs to know sizeof(struct timeval). By using the struct module I should be able to work out sizeof(long) from python, but I...
2
by: Tiger | last post by:
I try to write a struct into a brut file but I can't write all var in this struct when I try to add user I have that : > testmachine:/usr/share/my_passwd# ./my_passwd -a users.db > Ajout d'une...
7
by: nass | last post by:
hi all, i am running slackware linux and need to use some function that will will enable me to write and read from a shared mem segment.. i am using open() , to open a file, and then use mmap to...
24
by: Bill | last post by:
Hello, I'm trying to output buffer content to a file. I either get an access violation error, or crazy looking output in the file depending on which method I use to write the file. Can anyone...
63
by: Bill Cunningham | last post by:
I don't think I can do this without some help or hints. Here is the code I have. #include <stdio.h> #include <stdlib.h> double input(double input) { int count=0,div=0; double...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.