I'm trying to populate struct
values using system calls. My initial effort follows. However i get junk values from the print statement.
int fd;
int nbytes;
struct message {
char *from;
char *to;
int size;
};
struct message m1={"me","you",10};
struct message m2;
fd=creat("structfile",0644);
nbytes=write(fd,&m1,sizeof(m1));
read(fd,&m2,nbytes);
printf("%s %s %d",m2.from,m2.to,m2.size);
Is there another way to do this? (I'm thinking of the way that structures like hostent
and dirent
are filled up directly by making calls to gethostbyname
and readdir
respectively)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…