it’s possible to write non-char* by using write() function? I need to print a unsigned long and I have no idea how to do it. In other words, pass a unsigned long in buf parameter.
it’s possible to write non-char* by using write() function? I need to print a
Share
It’s usually preferable to use the standard C functions where available since they’re more portable. If you can, and you want to output it as text, you should look at
fprintfrather thanfwrite. The former will format it for you, the latter is meant for writing raw memory blocks.For example:
will output the text “42”.
If you do want to write out the binary representation,
fwriteis the means for that:That will write out the binary representation so that the bytes
0,0,0and42are written to the file (depending on what the memory layout is for anintvariable of course – it may vary depending on the implementation).That’s if you’re able to use file handles rather than descriptors, otherwise the
f*functions are no good for you. There may be valid reasons why you want to work with the lower levels.So, if all you have is a descriptor for
write, you’ll need to format the variable into a string first, with something like:That’s assuming you want it as text. If you want it as a binary value, it’s similar to the way we’ve done it above with the
fwrite: