1

I have following code

unsigned char input;
vector <unsigned char> buffer;
while (cin >>input){
    buffer.push_back(input);
}

for (int i=0;i<buffer.size();i++)
    cout<<std::hex<<(int)buffer.at(i)<<endl;

and I call the program with binary file

./program <<binary_file

which consits of: (view from hex editor)

0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f

However I get the following output:

0
1
2
3
4
5
6
7
8
e
f
10
11
12
13
14
15
16
17
18
19
1a
1b
1c
1d
1e
1f

As you can see, the "9","a","b","c","d" is missing. Could you explain what's wrong? Am I missing something essential? thanks

Or could you suggest any other way how to load binary file from cin and store it in

vector<unsigned char> buffer
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
user1762087
  • 440
  • 5
  • 16

1 Answers1

4

The characters 0x09 through 0x0D are whitespace, and as such are ignored by operator>> when extracting values. Use one of the std::cin.get() overloads if you want an unformatted character read (if you're programming on windows, carriage returns (0x0D) will still be ignored as part of a line end unless you reopen the standard input stream in binary mode).

You can also use the std::noskipws manipulator to disable leading whitespace skipping for formatted input functions.

user657267
  • 20,568
  • 5
  • 58
  • 77
  • could you please provide some code showing how to cin.get() this byte from cin? I'm struggling to get something else besides characters (a,b,c,d,e,f,g,h,i,...) – user1762087 Apr 29 '14 at 17:21
  • 1
    There's no standard way of putting `std::cin` into binary mode. (Also, at least until recently, 0x1A would be treated as end of file in text mode under Windows.) – James Kanze Apr 29 '14 at 17:22
  • 1
    @JamesKanze yes, but then again I think windows is the only platform that makes the text/binary distinction, and `_setmode` apparently works (not portable as you say). – user657267 Apr 29 '14 at 17:29
  • I updated the question, could you look at it? So the only thing is, that it can't be used on windows? – user1762087 Apr 29 '14 at 17:31
  • @user1762087: you can ask for clarification in comments, or make a new post. Please don't change what existing posts are asking. – Mooing Duck Apr 29 '14 at 17:34
  • @user1762087 it can be used on windows, but keep in mind that on streams opened in text mode on windows (`std::cin` is text only, although it is possible to non-portably reopen it in binary) the sequence 0x0D 0x0A is automatically converted to 0x0A. – user657267 Apr 29 '14 at 17:35
  • so on linux the sequence 0x0D 0x0A is read as 0x0D 0x0A ( = with no problem) and that's the only sequence on windows, which would be problematic? – user1762087 Apr 29 '14 at 17:39
  • @user1762087 as JamesKanze points out 0x1A might also terminate input in text mode on windows. – user657267 Apr 29 '14 at 17:43
  • OK, this program would be only used in linux, so I guess, it will be ok. Thanks for your help :) – user1762087 Apr 29 '14 at 17:45
  • @user1762087 You mean that Unix is about the only system which doesn't make the difference. _All_ of the non-Unix systems I've worked on have made the difference. Often, a much greater difference than that of Windows. – James Kanze Apr 29 '14 at 18:04
  • @JamesKanze I should have said in my (limited) experience, I'm not familiar with much outside of Unix and windows and assumed too much, thanks. – user657267 Apr 29 '14 at 18:09