2

I wrote to standard input and it appeared on screen (0 standard input, 1 standard output, 2 standard error

const int SIZE=12;

int main()
{
    int fd = open("input.txt", O_RDWR);
    char buffer[SIZE] = "Hello world";
    write(fd, buffer, SIZE - 1);

    lseek(fd, 0, SEEK_SET);
    char mem[SIZE];
    read(fd, mem, SIZE - 1);
    mem[SIZE] = '\0';

    write(0, mem, SIZE - 1);
    printf("\n");
    write(1, mem, SIZE - 1);
    printf("\n");
    write(2, mem, SIZE - 1);
    printf("\n");

    return 0;
}

Output:

Hello world
Hello world
Hello world

How this works? Doesn't standard input connected to keyboard?

Thanks.

user1289
  • 1,251
  • 2
  • 13
  • 25

4 Answers4

2

stdin and stdout often - on Linux systems at least - refer to the same entity in the filesystem, a single device file like /dev/tty2. Those "files" are generated by a specific driver, which is notified about any actions on that file.

When you read from it, the driver accesses (in some way, through possibly multiple other drivers) the keyboard you're sitting in front, and returns anything you type (filtered by the layers of software inbetween) as contents of the file.

When you're writing, then the driver knows that it needs to take different action, and directs your data through the software stack of drivers / display server / terminal emulator to finally have it displayed to you.

So when you write to stdin, all the driver sees is you writing to its device file, which means it should display what you write.


That said this is certainly in no way a portable behaviour, so don't expect this to happen on different platforms.

Daniel Jour
  • 15,896
  • 2
  • 36
  • 63
0

As I see it, once you have a fd, you can do whatever you want with it. stdin, stdout and stderr are const pointers declared in stdio.h so you dont need to open them manually, but you can "abuse" them and write into one and read from the other.

Indeed, there are systems where stdin is connected directly to the keyboard as said here: http://www.cplusplus.com/reference/cstdio/stdin/

for more info: http://pubs.opengroup.org/onlinepubs/009695399/functions/stdin.html

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
  • 1
    It's true, but why writing to standard input goes to screen? Doesn't it connected to keyboard? – user1289 Aug 25 '15 at 19:25
  • I edited an addition. There are systems where the stdin isn't connected directly to the keyboard. I'm assuming this is your case, too – CIsForCookies Aug 25 '15 at 19:27
  • That's not logical. I can't believe that when my program wants to read from standard input it reads something from my screen :) It should be connected to keyboard or some input device, not output. – user1289 Aug 25 '15 at 19:30
  • 1
    I'm quoting from another question: It's very possible that file descriptors 0, 1, and 2 are all open for both reading and writing (and in fact that they all refer to the same underlying "open file description"), in which case what you're doing will work. But as far as I know, there's no guarantee, so it also might not work. I do believe POSIX somewhere specifies that if stderr is connected to the terminal when a program is invoked by the shell, it's supposed to be readable and writable, but I can't find the reference right off.. – CIsForCookies Aug 25 '15 at 19:35
  • link to the question: http://stackoverflow.com/questions/7383803/writing-to-stdin-and-reading-from-stdout-unix-linux-c-programming – CIsForCookies Aug 25 '15 at 19:35
0

UNIX machines originally had consoles, typically connected by serial cables. As far as the operating system is concerned, the remote screen and keyboard are a single device, the "console", connected by a single link.

This design is still reflected in pretty much all operating systems. Imagine how crazy it would be if this worked differently. Say you run two Windows console applications. Should they be sharing the same keyboard and screen connection? Or should they each have their own console window that has a logical input stream and a logical output stream?

When you hit backspace on the keyboard to correct an error, should that back up the keyboard or the screen? They have to be the same device.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Can you please send some paper or something where I can read about this. This is a little confusing, why differentiate input and output is there is only one device for OS? – user1289 Aug 25 '15 at 19:46
  • @GrigorApoyan There are many devices. A Windows machine can have multiple text console windows, each with an input and output stream. A UNIX machine can have one or more local consoles, people connected over the network, virtual consoles emulated by software, and so on. – David Schwartz Aug 25 '15 at 19:53
0

I cannot recreate using Cygwin on Windows 7. Here's my program:

#include <cstdio>

int main(void)
{
    static const char   text_hello[] = "Hello fwrite to stdin.\n";
    fprintf(stdin, "This is from stdin.\n");
    fwrite(text_hello, 1, sizeof(text_hello) - 1, stdin);

    return 0;
}

Using g++ version 4.9.2. I get nothing displayed on the console window.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154