6

I'm trying to read from standard input and distinguish each character from one another by its decimal value. From what I understand, a Line Feed (10) and a Carriage Return (13) will be interpreted as the same character. I want to distinguish between the two. I know if I was reading from a file I could open it using the ios::binary parameter. But what about if I am reading from standard input?

jww
  • 97,681
  • 90
  • 411
  • 885
ordinary
  • 5,943
  • 14
  • 43
  • 60
  • If you know your platform, you could try to reopen `stdin` using an `ifstream` (the point I can't do it in a portable way, `/dev/stdin` should work for -ixes), then use `rdbuf` to hook that into `std::cin`. – Kos Jul 29 '12 at 23:11
  • See also http://stackoverflow.com/questions/7587595/read-binary-data-from-stdcin – Kos Jul 29 '12 at 23:13
  • Also see the discussion of using [`std::freopen`](https://stackoverflow.com/a/39758021/608639) (but it looks kind of hacky). It would be nice if someone provided a canonical answer with examples for BSDs, Linux, OS X, Solaris and Windows. – jww May 20 '18 at 18:27

1 Answers1

2

You can read from std::cin by using get. This method is specially designed for reading unformatted data (see doc)

Ivan Kruglov
  • 743
  • 3
  • 12
  • 1
    I think the problem is, `std::cin` is opened in text mode. There may be some unwanted translations due to text mode. – jww May 20 '18 at 18:29