C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 17th, 2006, 11:01 AM
honey006 honey006 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 55 honey006 User rank is Private First Class (20 - 50 Reputation Level)honey006 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 13 h 3 m 6 sec
Reputation Power: 7
Adding text tot .bin file

i have this program in C that writes a struct into a .bin file
but it i want it to add a new struct without deleting the old struct how do i do this
this is the function i have

i'm also trying to make it print that information afterwards
making a new screen for every struct,haven't got a clue how to do that
void klanten(int *klantennr)
{
KLANT_GEGEVENS klant;
FILE * klanten_file;
klanten_file=fopen("klanten.bin","wb");
(*klantennr)++;
fflush(stdin);
printf("voornaam:");
fgets(klant.voornaam, 20, stdin);
printf("achternaam:");
fgets(klant.naam, 20, stdin);
printf("straatnaam:");
fgets(klant.adres.straat, 20, stdin);
printf("nummer:");
scanf("%d", &klant.adres.nummer);
fflush(stdin);
printf("woonplaats:");
fgets(klant.adres.woonplaats, 20, stdin);
printf("postcode:");
scanf("%d", &klant.adres.postcode);
fflush(stdin);
klant.klantnummer=*klantennr;
fseek(klanten_file,0,SEEK_END);
fwrite(&klant,1,sizeof(klant),klanten_file);
fclose(klanten_file);
}

need help please asap
thank you
honey

Reply With Quote
  #2  
Old November 17th, 2006, 11:10 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,905 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 9 m 41 sec
Reputation Power: 1774
1. Use [code][/code] tags, so
void klanten(int *klantennr)
{
KLANT_GEGEVENS klant;
looks more like this
Code:
void klanten(int *klantennr)
{
    KLANT_GEGEVENS klant;


2. klanten_file=fopen("klanten.bin","wb");
Perhaps "ab" if you want to append to what's already there.
But you already seek to the end - so what's the question?

3. fflush(stdin);
Flushing input streams is undefined. Whatever it might do for you, I can tell you it doesn't work for me. If you're being good and using fgets() for everything (and not mixing it with scanf), then there should be no need for even trying to "flush the input stream"

4. fgets(klant.voornaam, 20, stdin);
I hope these structures contain real arrays, and not just char * pointers.
Though you should really do
Code:
char buff[BUFSIZ];
fgets( buff, sizeof buff, stdin );  // and check for errors
// validate the buff in some way
strcpy( klant.voornaam, buff );


5. scanf("%d", &klant.adres.postcode);
Yeah, mixing fgets and scanf - bad voodoo there.
Use fgets() + sscanf() if you need to do this.
Also, how long are your postcodes, do they have significant leading zeros? Like phone numbers, it's probably not good to store them as ints.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Adding text tot .bin file

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap