Hi, i'm trying to copy a struct into a binary file using the unix instruction write, so i declare and fill the struct "superbloque" in one function "initSB" and then i pass the pointer to another function called bwrite (for block write) which calls write. The problem is that i call the function with a pointer to my struct and write only creates the file and doesn't write anything. I'm pretty sure the problem i have has to do with the pointers because when i execute
printf("%i",sizeof(*pointer_to_the_struct)
in bwrite function it says 1 and should be 48
Here are the important parts of the code:
struct superbloque{
unsigned int firstmbits;
unsigned int lastmbits
...
unsigned int numbloques;
}
int initSB (unsigned int nbloq){
struct superbloque SB;
SB.firstmbits=0;
SB.lastmbits=TamMB(nbloq)-1;
...
SB.numbloques=nbloq;
bwrite(0,&SB)
}
int bwrite(unsigned int bloque, void *buf){
lseek(mifd, bloque*tbloque, SEEK_SET);
return write(mifd, buf, sizeof(*buf);
}
I know there's a lot of information about this but i've been reading for two days and I can't figure out how to solve the problem.
Thank you very much for your help
Ricardo Galli
Sistemas operativos
Universidad de las Islas Baleares (Palma de Mallorca, Spain)
if pointer_to_the_struct is a pointer to the structure, then *pointer_to_the_struct is the first byte in the structure. so you a integer of 1
you should try
printf("%i",sizeof(struct xxx))
and this will get you the real size of the structure, and that's what you want