1
vector<byte> h;
ifstream fin(file.c_str(), ios::binary);
if(!fin)
    return false;
byte b;
while(fin >> b)
    h.push_back(b);

The length of h is 4021, while the raw file length is 4096 bytes. But the code below gives a string of 4096 bytes. Why?

ostringstream sout;
sout << fin.rdbuf();
string s = sout.str();

UPDATES:

@user2079303 solved my problem, but any other way to perform the reading task. It's too easy to get it wrong.

5
  • Can you shorten the file such that a bas64 encoded version is short enough to add it to the question while still exhibiting the problem? Commented Feb 20, 2014 at 8:19
  • Are you sure that raw file length is 4096 bytes exactly? Default blocksize on some filesystems is 4096 bytes. Any file smaller than the block size will take one blocks worth of hard drive space in usual filesystems. Commented Feb 20, 2014 at 8:22
  • @user2079303 Yes, exactly 4096 bytes. And the missing data is in the middle somewhere. I use fin.tellg() to output the current position. Find out that when I got the the 2000 bytes, the vector length is 1995. Commented Feb 20, 2014 at 8:24
  • Where does the byte type come from? Commented Feb 20, 2014 at 8:25
  • @Oswald windows.h maybe defined a micro like #define unsigned char byte. I'm programming using win32. Commented Feb 22, 2014 at 1:42

1 Answer 1

6

When reading input stream char by char (your bytes are chars, right?), standard streams ignore whitespace by default. You can use std::noskipws to stop ignoring them.

fin >> std::noskipws >> b;

Note: ios::binary has no effect on this behaviour even though one might expect so. It only disables translating line-endings as far as I know.

If you don't want to worry about processing, you can use functions that fulfill UnformattedInputFunction. cppreference has a nice example on how to read a binary file with istream::read.

Sign up to request clarification or add additional context in comments.

3 Comments

But any other way to perform the task. I can easily forget this way.
@zoujyjs: Another way is to use istream::read which is much less pretty, but doesn't process the input like the stream operator does and is probably much faster too since you can read the whole file in one go rather than byte by byte.
God bless this man, I have been struggling with this issue for 2 days.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.