The write()
function expects a void *
for its buffer; it writes arbitrary binary data. If you need conversion to string, use printf()
.
You don't show the declaration of userid
, but the write()
line should be written as:
if (write(fd, &userid, sizeof(userid)) != sizeof(userid))
This will detect short writes (unlikely to be a problem for an integer type) and other problems, and works correctly regardless of the type of userid
. The original version of the line is arguably buggy, therefore. Otherwise, the bug seems to be in your expectations rather than the code per se.