zizi21
asked on
fwrite - bus error
hi,
i am using fwrite like this:
fwrite(str,1,sizeof(str),f p) 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
i am using fwrite like this:
fwrite(str,1,sizeof(str),f
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
how is id declared?
ASKER
int id for 0, 1
char *str for id1, id2
char *str for id1, id2
post sample code please.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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),f p) ;
fwrite(id,1,sizeof(id),fp) ;
char *str ={"hello", "hello2"};
int id=0;
fwrite(str,1,sizeof(str),f
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.
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);
But perhaps you are thinking of
fprintf(fp,"%d ",id);
ASKER
thanks