What is the correct way to work out how many bytes an int is? and how do I write an int to a file descriptor?
Here is a mock code sample which might make clear what I am trying to achieve:
char *message = "test message";
int length = strlen(message);
int fd = open(file, O_CREAT|O_RDWR);
write(fd, length??, ??); // <--- what goes here
write(fd, message, length);
I dont care about platform independence and byte order, just that it can compile on as many platforms as possible.
sizeof(length)
goes in the field.
It is preferable over using sizeof(int)
in case you ever change the type of length
in the future.
sizeof
expresses the, well, size of a data type in multiples of sizeof(char)
, which is always 1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With