0

I'm having difficulity understanding the read function in C.

len = read(fd, buf, 32);

when I assign fd as 0,1,2 and run the program, its basically doing the same thing, can someone tell me what difference does this make?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Xiang Ramadhan
  • 115
  • 1
  • 4
  • 10

2 Answers2

2

read() attempts to read up to count bytes from file descriptor fd.

fd = 0
fd = 1
fd = 2

Is reading from different file descriptors. The difference is, you are reading from different files, and the data read into the buffer is different.

What is the difference in reading from Book A and reading from Book B ? it is the same process of reading a book... it is the content that changes.

Tony Tannous
  • 14,154
  • 10
  • 50
  • 86
  • Different file descriptors may refer to the same file – JFMR Aug 08 '17 at 08:06
  • wait .. if im running it on cmd, and its asking for input, what "book" is it? sorry im a newbie in this thing , i only do competitive programming ... – Xiang Ramadhan Aug 08 '17 at 08:07
  • @眠りネロク true, but lets assume it's different files. – Tony Tannous Aug 08 '17 at 08:07
  • @XiangRamadhan, reading from stdin, will read data you type in the `cmd`. For simplicity forget `0 1 2`, think as if you are opening two file.. (they will have different file descriptors). if one file content is `blabla` and the other one is `foo`, and you want to read 2 bytes, the first one will return `bl` and the other one `fo`. – Tony Tannous Aug 08 '17 at 08:09
  • what? so what does fd do in this ? if i assign fd into more than 2 it would not take any input . – Xiang Ramadhan Aug 08 '17 at 08:13
  • @XiangRamadhan linux limits the number of file descriptors a process can have. So you can't assign any `fd`. Read https://stackoverflow.com/questions/5256599/what-are-file-descriptors-explained-in-simple-terms it might help. – Tony Tannous Aug 08 '17 at 08:15
  • still unclear :( ... so why is read(2,buf,32) taking something from stdin? – Xiang Ramadhan Aug 08 '17 at 08:28
  • @XiangRamadhan try https://stackoverflow.com/questions/45536707/strange-behavior-performing-library-functions-on-stdout-and-stdins-file-descrip – Tony Tannous Aug 08 '17 at 08:30
  • 1
    this and @Art comment answered it , thanks so much – Xiang Ramadhan Aug 08 '17 at 08:34
1

As far as I understand your question it is why nothing changes if you read from file descriptors 0, 1, 2.

In a normal program the file descriptor 0 is stdin, 1 is stdout and 2 is stderr. stdin is where you should read your input, 1 is where you should write your output and 2 is where you should write your error messages.

It is not uncommon that all three file descriptors may point to the same underlying file (a file can also be the console, network connection, etc.) behind the scenes. If you're just running your program from the command line this is actually quite likely. In that case you may be able to read from all of them and get the exact same result.

But. Then you decide that you want to save the output of the program in a file and run it like this: program > output. Now file descriptor 1 is no longer pointing to the same file as stdin and your program would break. Same thing happens if you point stderr to some error logging facility. Or get the input from a file or a pipe. Or run the program in some debuggers. Or a different terminal. This is why you should only read from 0 and no other file descriptors, even if you might get away with it sometimes.

Art
  • 19,807
  • 1
  • 34
  • 60