Skip to main content

Command Palette

Search for a command to run...

std::ifstream skips white spaces

Published
•1 min read•View as Markdown

I needed to read a binary ppm. Normally, I would normally write an ASCII ppm, but I didn't have a choice because I as converting a png to ppm with ImageMagick. Why PPM? Well, mostly because it doesn't require a library to link (or statically compile against) to read png.

It's been a while since I used std::ifstream to read binary data with std::ios::binary. And there's one thing I forgot: std::ifstream will skip over white space.

How do you get around that? By using std::noskipws.

std::vector<unsigned char> data;
 std::ifstream file("../../whatever.ppm", std::ios::binary);
if (file.is_open())
{
    unsigned char byte;
    for (int i=0; i<15; i++){
        file >> std::noskipws >> byte;
}
int i = 0;
while (file >> byte) {
    data.push_back(byte);
}
file.close();

More from this blog

It needs to be written down

59 posts