Given double temp = 6.5; how could I pass it to the UNIX write() system call:
write(fd[1], (WTF?), 100)
fd[1]is a file descriptor.100is a fixed buffer size to handle large number values, just in case.
What do I put it in the middle? I’ve tried itoa, &temp, *temp, (char) temp, and a few other ways by googling my way to different solutions, but with no luck so far. Honestly I don’t even know what type is const void *. I just know it will stop complaining if I pass a string directly such as “Hello World” to the 2nd argument.
The second parameter to
writeis a pointer to some location in memory, and the third parameter is the number of bytes from that location in memory that you want to write to the file. So your code would be:I’m not sure if this is exactly what you want, though. To be clear, this will write the binary representation of your
doubleto the file. On most platforms, that will be the following series of bytes (represented here in hexadecimal):If this is supposed to be a text file, then you’ll want to convert your number to a string first.
This would write “6.5” to your file.