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 oflengthin the future.sizeofexpresses the, well, size of a data type in multiples ofsizeof(char), which is always 1.