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);

4 Answers4

5

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.

Desmond Hume
  • 8,037
  • 14
  • 65
  • 112
  • 2
    @ 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. :) – catfish_deluxe_call_me_cd Sep 18 '11 at 18:40
  • Yeah, good point :P I've already gotten softer on it in the edit. – Desmond Hume Sep 18 '11 at 19:07
  • 1
    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 =) – Desmond Hume Sep 18 '11 at 19:34
  • Well if it happens for `open()` I think it's reasonably safe to assume it also happens for the constructor. – catfish_deluxe_call_me_cd Sep 19 '11 at 05:57
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

Dave
  • 10,964
  • 3
  • 32
  • 54
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
sehe
  • 374,641
  • 47
  • 450
  • 633
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.

Shuai
  • 962
  • 1
  • 10
  • 21