int main(int argc, char **argv)
{
double s = 43;
write(1, &s, sizeof(double));
}
This is the output : �E@
int num = 1234;
write(STDOUT_FILENO, &num, sizeof(num));
I have the same issue too, output : �
CodePudding user response:
write
does not convert the value to text before writing it. What you are doing is writing the binary value of the double to a file.
You should convert it to a string before, like so :
int main(int argc, char **argv)
{
double s = 43;
char buff[26]; //Buffer to store the string
snprintf(buff, 16, "%f", s); //Convert the float to a string
write(1, &s, strlen(buff));
}
Note the use of snprintf. It works like printf, but writes to an array instead. sprintf would also work but can be unsafe as there are no limit to the number of characters written, which can cause segmentation faults.