Is ios::in needed for ifstream's opened in binary mode?
Asked Answered
Y

4

8

What's the difference between these two? Isn't the in flag object thing redundant? Thanks.

std::ifstream file1("one.bin", std::ifstream::in | std::ifstream::binary);

std::ifstream file2("two.bin", std::ifstream::binary);

Yellowtail answered 18/9, 2011 at 18:18 Comment(2)
erm... from the docs I don't suppose it is redundant; and it's worth 30 seconds to ... try :)Regine
Why is it relevant if its binary or not?Visually
S
6

From the docs on ifstream class constructor:

binary (binary) Consider stream as binary rather than text.
in (input) Allow input operations on the stream.

So when reading from a file, I would use std::ifstream::in flag not because it's required (or not) but because it would be a good programming practice to let a programming interface know what you are going to use it for.

Edit:
The following is taken from http://www.cplusplus.com/doc/tutorial/files/, about open() member function though (but the constructors in the code in the question probably call open() copying the mode flags without modification).

class: default mode parameter
ofstream: ios::out
ifstream: ios::in
fstream: ios::in | ios::out

For ifstream and ofstream classes, ios::in and ios::out are automatically and respectively assumed, even if a mode that does not include them is passed as second argument to the open() member function.

Nevertheless, many examples over the Web use ifstream::in when showing a construction of an ifstream object. Could really be some kind of a superstition practice, instead of a programming one.

Staffman answered 18/9, 2011 at 18:35 Comment(4)
@ Naveen: Do I need to let ifstream know I want to use it for input? I could just as well use plain fstream then - which is why I posted this question in the first place. :)Yellowtail
Yeah, good point :P I've already gotten softer on it in the edit.Staffman
Guys, stop upvoting my answer because it doesn't really give much of an answer, just expresses a personal opinion plus gives some indirectly related info on open() member function. However, it doesn't mean I want it to get downvoted =)Staffman
Well if it happens for open() I think it's reasonably safe to assume it also happens for the constructor.Yellowtail
J
2

binary, in this case, only refers to the method of reading or writing. In regular mode on windows, '\n' is translated to '\r''\n'. This can affect both reading and writing, so binary mode turns this off. out|binary makes just as much sense as in|binary

Jazzman answered 18/9, 2011 at 18:35 Comment(0)
R
1

I can't find authoritative documentation online.

Edit I can't even find a proper reference in my copy the Josuttis Book, 8th printing. It should have been in section 13.9 pp. 627-631

Empirical evidence suggests it is redundant IFF none of std::ios::in or std::ios:out are passed:

#include <fstream>
#include <iostream>

int main(int argc, char** args)
{
    std::ifstream ifs(args[0], std::ios::binary);
    std::cout << ifs.rdbuf() << std::flush;

    return 0;
}

Succeeds:

test | md5sum
md5sum test

show the same hash sum.


    // ...
    std::ifstream ifs(args[0], std::ios::out | std::ios::binary);

will fail (zero bytes output)

test | wc -c  # shows 0
Regine answered 18/9, 2011 at 18:29 Comment(0)
S
0

From cplusplus.com reference page, there is no difference.

in is always set for ifstream objects (even if explicitly not set in argument mode).

It's the same for ofstream. Therefore, you don't need to set std::ios::in for ifstream or std::ios::out for ofstream, even if you have set std::ios::binary which omits the in/out mode.

Schwitzer answered 26/7, 2021 at 2:57 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.