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)
the rest of the relevant code to add is here,
here is the code to read the file
please help me out
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)
Code:
struct Employee_Details
{ char [20]
long int
float
char [20]
time_t
}emp,temp;
Code:
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
Code:
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