We help IT Professionals succeed at work.

fwrite - bus error

zizi21
zizi21 asked
on
778 Views
Last Modified: 2012-05-05
hi,

i am using fwrite  like this:

fwrite(str,1,sizeof(str),fp) and it works

but i am having problem when i try to use like this:
fwrite(id,1,sizeof(id),fp);


actually, what i am trying to do is to write data like this

0 id1
1 id2
etc...

id1 is stored in str and 0 is id in integer...

i think, i cant write in integer..could anyone pls help? it was fine when tried using text file with fprintf but have a problem with fwrite....

thanks


Comment
Watch Question

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
how is id declared?

Author

Commented:
int id for 0, 1
char *str for id1, id2
post sample code please.
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
this is just test file first because i am trying to learn on how to write to a binary file and read it later....

char *str ={"hello", "hello2"};
int id=0;

fwrite(str,1,sizeof(str),fp) ;
fwrite(id,1,sizeof(id),fp);




fwrite(&id,1,sizeof(id),fp);
char *str ={"hello", "hello2"}; <-- this is declared wrong;

char* is a pointer to a series of characters. IE 1 string.
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
fwrite(&id,1,sizeof(id),fp);
But perhaps you are thinking of
fprintf(fp,"%d ",id);

Author

Commented:
thanks
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.