Why should I ever open a file (std :: ifstream) without std :: ios :: binary?

This may apply to another part of Stack Exchange, but I donโ€™t think so - .se programmers are more about other things.

Going to the question: there are things that you can do with std :: ios :: binary that you cannot do in text mode (for example, relative search), but I cannot find anything in text mode that you can't do it in binary mode - even reading a file as text, for example, standard :: GetLine ()

So why should I ever open text? How is a related question possible, why not open as a binary by default? Whose precedent violates?

EDIT Additional Information

Here is what made me ask:

I have a file that is created on a Windows system, i.e. line contours CR LF.

I open it with std::ifstream using the std::ios::binary flag

I scan the file with std::getline and get exactly the behavior I would expect - getline reads one line at a time.

System: Windows 7 Pro

Compiler: g ++ for MINGW32

+7
c ++ fstream
source share
2 answers

What can you do in text mode, which you cannot do in binary format? Read the text, for starters. A file opened in text mode automatically translates between the '\n' character inside and the system uses it to delimit lines in files from the outside. It can also recognize an arbitrary end of a file, even if the underlying system requires that the file sizes be concise fixed size.

The choice today is somewhat complicated by the fact that you often have to access files from incompatible systems. If you have a file system mounted on both Windows and Unix, write it as text for Windows, and read it as text for Unix, then you will see additional characters. In such cases, it may be preferable to read and write binary files, as well as to draw line output while controlling yourself, in accordance with any conventions that you prefer. Similarly, if the โ€œfileโ€ is actually a socket with another machine, you will want to open it in binary format and manually terminate the line yourself, in accordance with the protocol requirements.

+6
source share

The stdin well opens by default in text mode, this allows you to use, for example, CTRL + Z for the EOF signal, so I donโ€™t understand why you think that there is no need for threads open in anyting, except for binary mode.

+2
source share

All Articles