t1497f35
April 9th, 2012, 05:33 AM
Hi,
I was toying around using "write()" instead of "printf()" to write a string directly to STDOUT_FILENO and STDERR_FILENO, however, I noticed that "write" prints the string "hi" to the console even if I write it to STDIN_FILENO! Why? Imho it shouldn't print out anything in this case.
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
const char *buf = "hi\n";
ssize_t count = write(STDIN_FILENO, buf, strlen(buf));
if(count < 0) {
perror("Write");
}
exit(0);
}
I was toying around using "write()" instead of "printf()" to write a string directly to STDOUT_FILENO and STDERR_FILENO, however, I noticed that "write" prints the string "hi" to the console even if I write it to STDIN_FILENO! Why? Imho it shouldn't print out anything in this case.
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
const char *buf = "hi\n";
ssize_t count = write(STDIN_FILENO, buf, strlen(buf));
if(count < 0) {
perror("Write");
}
exit(0);
}