Link to home
Create AccountLog in
Avatar of zizi21
zizi21

asked on

fwrite - bus error

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


Avatar of ozo
ozo
Flag of United States of America image

how is id declared?
Avatar of zizi21
zizi21

ASKER

int id for 0, 1
char *str for id1, id2
post sample code please.
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of zizi21

ASKER

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.
fwrite(&id,1,sizeof(id),fp);
But perhaps you are thinking of
fprintf(fp,"%d ",id);

Avatar of zizi21

ASKER

thanks