5

This question concerns the behaviour of ifstream and ofstream when reading and writing data to files.

From reading around stackoverflow.com I have managed to find out that operator<< (stream insertion operator) converts objects such as doubles to text representation before output, and calls to read() and write() read and write raw data as it is stored in memory (binary format) respectively. EDIT: This much is obvious, nothing unexpected here.

I also found out that opening a file in binary mode prevents automatic translation of newline characters as required by different operating systems.

So my question is this: Does this automatic translation, eg; from \n to \r\n occur when calling functions read() and write()? Or is this behaviour just specific to the operator<<. (And also operator>>.)

Note there is a similar but slightly less specific question here. It does not give a definite answer. Difference in using read/write when stream is opened with/without ios::binary mode

FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
  • 1
    [Binary and text modes](http://en.cppreference.com/w/cpp/io/c#Binary_and_text_modes) – πάντα ῥεῖ Jun 12 '15 at 11:08
  • 2
    @πάνταῥεῖ That does not answer the question. It seems that the OP understood the difference between binary and text mode. The problem is that in C++'s stream's there's the additional concept of Formatted and Unformatted I/O. As I understood it, this question is about how those two interact. – ComicSansMS Jun 12 '15 at 11:11
  • So does this behaviour also apply to read() / write() or not? – FreelanceConsultant Jun 12 '15 at 11:11
  • @ComicSansMS Yes - essentially I want to know if I create a character buffer full of things which look like '\n' characters, then if I call write() on this buffer to write it to disk, WHAT will exactly appear in my file on disk? IE: Will it be a load of '\n's or will it be something twice as long with CRLF's on say a Windows OS? – FreelanceConsultant Jun 12 '15 at 11:12
  • @ComicSansMS That's also well explained in this reference just one or two clicks away. The question clearly is a sign of lacking research efforts. – πάντα ῥεῖ Jun 12 '15 at 11:13
  • Hardly - if the info isn't clearly presented then that's probably why I didn't find it. I've been using C++ for many years now and only just stumbled across this. Could someone clarify for me rather than redirecting me to other webpages where the info may or may not be there and if it is then it may or may not be buried amongst pages of text. – FreelanceConsultant Jun 12 '15 at 11:18
  • My assumption is that read() and write() do the sensible thing of NO TRANSLATIONS regardless of OS, but that's just my opinion on what would be sensible behaviour to my mind - the reality may be different. IE: They write binary data in the fastest possible way and never look to see if any block of 8 bits looks like a '\n'. – FreelanceConsultant Jun 12 '15 at 11:19
  • Not really sure how someone's decided to close vote this for "off topic"? It's a programming question, about the C++ standard library? Seems exactly "on topic" to me... – FreelanceConsultant Jun 16 '15 at 13:34

1 Answers1

4

The difference between binary and text mode its at a lower level.

If you open a file in text mode you will get translated data even when using read and write operations.

Please also note that you're allowed to seek to a position in a text file only if the position was obtained from a previous tell (or 0). To be able to do random positioning, the file must have been opened in binary mode.

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
6502
  • 112,025
  • 15
  • 165
  • 265