Need help in Unix C programming

hey guys.
im currently trying to make a program in unix (redhat, compiling the .c file using gcc) and i need urgent urgent help.

i need to save objects of structures in a file but they dont seem to be saving properly. ive been on it for a whole day now. dunno why i cant do it.
when i try to read, it reads the first search i give it but after that i start getting garbage

here is the structure(names of variables omitted)

struct Employee_Details
{       char [20]
        long int 
        float 
        char [20]
        time_t 
}emp,temp;

the rest of the relevant code to add is here,

recv(sd,&emp,sizeof(emp),0);
fd=open("DB.txt",O_RDWR|O_CREAT,0766);
lseek(fd,0,SEEK_END);
write(fd,&emp,sizeof(emp));
send(sd,"Added",6,0);

here is the code to read the file

fd=open("DB.txt",O_RDWR|O_CREAT,0766);
recv(sd,&emp,sizeof(emp),0);
lseek(fd,0,SEEK_SET);
while(flag) /* flag=1*/
{       read(fd,&temp,sizeof(temp));
         if(temp.INT_MEMBER==emp.INT_MEMBER)
        {       flag=0;
                 send(sd,&temp,sizeof(temp),0);
                 lseek(fd,-(sizeof(temp)),SEEK_CUR);
                 temp.TimeStamp=time(NULL);
                 write(fd,&temp,sizeof(temp));
        }
}

please help me out

Are you checking the return codes from your calls? Especially to recv()?

ya i am. put perror's everywhere. also checking return values.
but when i did that today another problem was found. im using sockets so when a server sends an ogject to client, it sends X bytes of data but client always receives X-4 bytes of data, thus the values never match...

If you're not receiving all the data, do another read() to get the rest. Like any stream, you're not guaranteed it all in one go. If you've been assuming you always get it all at once, that's probably where the garbage is coming from too -- writing data you never got, and starting halfway through other things when you start the "next" struct. If you're sending fixed size data structures back and forth you might consider packet sockets instead of stream ones; UDP packets will always arrive whole. They can't hold much more than one kilobyte per packet though.

Also, please post your actual code. If you have perrors everywhere then you clipped an awful lot of error checking to post that here; most of our suggestions would be "check z's return value" when you're already doing so. :wink:

^thnx man. will do tht.
my code is pretty messed up right now with loads of comments n all. should i still post it?