What's the difference between opening a file with ios::binary or ios::out or both?












17














I'm trying to figure out the difference between opening a file like:



fstream *fileName*("FILE.dat",ios::binary);


or



fstream *fileName*("FILE.dat",ios::out);


or



fstream *fileName*("FILE.dat",ios::binary | ios::out);


I found that all of these forms are identical: in all cases, the same output on the file is produced using either *fileName*<< or *fileName*.write().










share|improve this question





























    17














    I'm trying to figure out the difference between opening a file like:



    fstream *fileName*("FILE.dat",ios::binary);


    or



    fstream *fileName*("FILE.dat",ios::out);


    or



    fstream *fileName*("FILE.dat",ios::binary | ios::out);


    I found that all of these forms are identical: in all cases, the same output on the file is produced using either *fileName*<< or *fileName*.write().










    share|improve this question



























      17












      17








      17


      3





      I'm trying to figure out the difference between opening a file like:



      fstream *fileName*("FILE.dat",ios::binary);


      or



      fstream *fileName*("FILE.dat",ios::out);


      or



      fstream *fileName*("FILE.dat",ios::binary | ios::out);


      I found that all of these forms are identical: in all cases, the same output on the file is produced using either *fileName*<< or *fileName*.write().










      share|improve this question















      I'm trying to figure out the difference between opening a file like:



      fstream *fileName*("FILE.dat",ios::binary);


      or



      fstream *fileName*("FILE.dat",ios::out);


      or



      fstream *fileName*("FILE.dat",ios::binary | ios::out);


      I found that all of these forms are identical: in all cases, the same output on the file is produced using either *fileName*<< or *fileName*.write().







      c++ fstream






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 2 '15 at 0:58









      Jamal

      60662229




      60662229










      asked Feb 8 '10 at 23:08









      Alan_AI

      72631329




      72631329
























          2 Answers
          2






          active

          oldest

          votes


















          23














          ios::out opens the file for writing.



          ios::binary makes sure the data is read or written without translating new line characters to and from rn on the fly. In other words, exactly what you give the stream is exactly what's written.






          share|improve this answer





















          • and what does it mean to use both?
            – Alan_AI
            Feb 8 '10 at 23:36










          • Well, you'd be writing to a file without translating any characters.
            – Nick Bedford
            Feb 9 '10 at 0:04






          • 2




            So if I don't mark an ifstream as binary, and read, say 10 doubles from it, and Windows finds a random 'n' in the data, then the stream get expanded from 10*sizeof(double) to 10*sizeof(double) +1 as a r is inserted, and overwrites then end of my double buffer?
            – user14717
            Apr 10 '15 at 13:46






          • 1




            is it good practice to use ios::binary all the time?
            – FluorescentGreen5
            Oct 13 '16 at 4:53



















          5














          Opening a file with ios::binary controls how newline characters are handled. On Windows, they are expanded to CRLF pairs. That's it - it has no effect on how things like operator<< work.






          share|improve this answer





















          • but what are CLRF pairs?
            – Alan_AI
            Feb 8 '10 at 23:30






          • 2




            CLRF stands for carriage-return, line feed. These are the two bytes used to specify a new line in Windows text encoding. It's mostly redundant because on a computer, you really only need a new-line character.
            – Nick Bedford
            Feb 9 '10 at 0:05






          • 3




            Long time ago, in the days of Teletypes and typewriters, output machines had carriages that moved left to write as characters were printed. One command, Carriage Return, moved the carriage back to the left. Another command, Linefeed, advanced the paper to the next line. These two commands could be executed independently so that the paper advanced mid-line (using Linefeed) or rewriting the current line (using Carriage Return). As a pair, they cause the printing to start at the left margin of the next line.
            – Thomas Matthews
            Feb 9 '10 at 0:26






          • 6




            Unix people, being impatient typists, decided that the computer should handle both Carriage Returns and Linefeeds, improving productivity by typing less characters. This new command was called Newline. On some output systems you could see the carriage move left and the paper advance for each Newline, including blank lines. The C language decided to make peace and let the OS provide translations (without ios::binary) or provide no translations (with ios::binary). The ios::out determines data direction (out from the computer).
            – Thomas Matthews
            Feb 9 '10 at 0:32












          • thank u mr. Thomas so can u give me one difference between using ios::binary and ios::out | ios::binary for opening a file r nt they identical?
            – Alan_AI
            Feb 9 '10 at 1:22











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2225600%2fwhats-the-difference-between-opening-a-file-with-iosbinary-or-iosout-or-bot%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          23














          ios::out opens the file for writing.



          ios::binary makes sure the data is read or written without translating new line characters to and from rn on the fly. In other words, exactly what you give the stream is exactly what's written.






          share|improve this answer





















          • and what does it mean to use both?
            – Alan_AI
            Feb 8 '10 at 23:36










          • Well, you'd be writing to a file without translating any characters.
            – Nick Bedford
            Feb 9 '10 at 0:04






          • 2




            So if I don't mark an ifstream as binary, and read, say 10 doubles from it, and Windows finds a random 'n' in the data, then the stream get expanded from 10*sizeof(double) to 10*sizeof(double) +1 as a r is inserted, and overwrites then end of my double buffer?
            – user14717
            Apr 10 '15 at 13:46






          • 1




            is it good practice to use ios::binary all the time?
            – FluorescentGreen5
            Oct 13 '16 at 4:53
















          23














          ios::out opens the file for writing.



          ios::binary makes sure the data is read or written without translating new line characters to and from rn on the fly. In other words, exactly what you give the stream is exactly what's written.






          share|improve this answer





















          • and what does it mean to use both?
            – Alan_AI
            Feb 8 '10 at 23:36










          • Well, you'd be writing to a file without translating any characters.
            – Nick Bedford
            Feb 9 '10 at 0:04






          • 2




            So if I don't mark an ifstream as binary, and read, say 10 doubles from it, and Windows finds a random 'n' in the data, then the stream get expanded from 10*sizeof(double) to 10*sizeof(double) +1 as a r is inserted, and overwrites then end of my double buffer?
            – user14717
            Apr 10 '15 at 13:46






          • 1




            is it good practice to use ios::binary all the time?
            – FluorescentGreen5
            Oct 13 '16 at 4:53














          23












          23








          23






          ios::out opens the file for writing.



          ios::binary makes sure the data is read or written without translating new line characters to and from rn on the fly. In other words, exactly what you give the stream is exactly what's written.






          share|improve this answer












          ios::out opens the file for writing.



          ios::binary makes sure the data is read or written without translating new line characters to and from rn on the fly. In other words, exactly what you give the stream is exactly what's written.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 8 '10 at 23:12









          Nick Bedford

          3,7442432




          3,7442432












          • and what does it mean to use both?
            – Alan_AI
            Feb 8 '10 at 23:36










          • Well, you'd be writing to a file without translating any characters.
            – Nick Bedford
            Feb 9 '10 at 0:04






          • 2




            So if I don't mark an ifstream as binary, and read, say 10 doubles from it, and Windows finds a random 'n' in the data, then the stream get expanded from 10*sizeof(double) to 10*sizeof(double) +1 as a r is inserted, and overwrites then end of my double buffer?
            – user14717
            Apr 10 '15 at 13:46






          • 1




            is it good practice to use ios::binary all the time?
            – FluorescentGreen5
            Oct 13 '16 at 4:53


















          • and what does it mean to use both?
            – Alan_AI
            Feb 8 '10 at 23:36










          • Well, you'd be writing to a file without translating any characters.
            – Nick Bedford
            Feb 9 '10 at 0:04






          • 2




            So if I don't mark an ifstream as binary, and read, say 10 doubles from it, and Windows finds a random 'n' in the data, then the stream get expanded from 10*sizeof(double) to 10*sizeof(double) +1 as a r is inserted, and overwrites then end of my double buffer?
            – user14717
            Apr 10 '15 at 13:46






          • 1




            is it good practice to use ios::binary all the time?
            – FluorescentGreen5
            Oct 13 '16 at 4:53
















          and what does it mean to use both?
          – Alan_AI
          Feb 8 '10 at 23:36




          and what does it mean to use both?
          – Alan_AI
          Feb 8 '10 at 23:36












          Well, you'd be writing to a file without translating any characters.
          – Nick Bedford
          Feb 9 '10 at 0:04




          Well, you'd be writing to a file without translating any characters.
          – Nick Bedford
          Feb 9 '10 at 0:04




          2




          2




          So if I don't mark an ifstream as binary, and read, say 10 doubles from it, and Windows finds a random 'n' in the data, then the stream get expanded from 10*sizeof(double) to 10*sizeof(double) +1 as a r is inserted, and overwrites then end of my double buffer?
          – user14717
          Apr 10 '15 at 13:46




          So if I don't mark an ifstream as binary, and read, say 10 doubles from it, and Windows finds a random 'n' in the data, then the stream get expanded from 10*sizeof(double) to 10*sizeof(double) +1 as a r is inserted, and overwrites then end of my double buffer?
          – user14717
          Apr 10 '15 at 13:46




          1




          1




          is it good practice to use ios::binary all the time?
          – FluorescentGreen5
          Oct 13 '16 at 4:53




          is it good practice to use ios::binary all the time?
          – FluorescentGreen5
          Oct 13 '16 at 4:53













          5














          Opening a file with ios::binary controls how newline characters are handled. On Windows, they are expanded to CRLF pairs. That's it - it has no effect on how things like operator<< work.






          share|improve this answer





















          • but what are CLRF pairs?
            – Alan_AI
            Feb 8 '10 at 23:30






          • 2




            CLRF stands for carriage-return, line feed. These are the two bytes used to specify a new line in Windows text encoding. It's mostly redundant because on a computer, you really only need a new-line character.
            – Nick Bedford
            Feb 9 '10 at 0:05






          • 3




            Long time ago, in the days of Teletypes and typewriters, output machines had carriages that moved left to write as characters were printed. One command, Carriage Return, moved the carriage back to the left. Another command, Linefeed, advanced the paper to the next line. These two commands could be executed independently so that the paper advanced mid-line (using Linefeed) or rewriting the current line (using Carriage Return). As a pair, they cause the printing to start at the left margin of the next line.
            – Thomas Matthews
            Feb 9 '10 at 0:26






          • 6




            Unix people, being impatient typists, decided that the computer should handle both Carriage Returns and Linefeeds, improving productivity by typing less characters. This new command was called Newline. On some output systems you could see the carriage move left and the paper advance for each Newline, including blank lines. The C language decided to make peace and let the OS provide translations (without ios::binary) or provide no translations (with ios::binary). The ios::out determines data direction (out from the computer).
            – Thomas Matthews
            Feb 9 '10 at 0:32












          • thank u mr. Thomas so can u give me one difference between using ios::binary and ios::out | ios::binary for opening a file r nt they identical?
            – Alan_AI
            Feb 9 '10 at 1:22
















          5














          Opening a file with ios::binary controls how newline characters are handled. On Windows, they are expanded to CRLF pairs. That's it - it has no effect on how things like operator<< work.






          share|improve this answer





















          • but what are CLRF pairs?
            – Alan_AI
            Feb 8 '10 at 23:30






          • 2




            CLRF stands for carriage-return, line feed. These are the two bytes used to specify a new line in Windows text encoding. It's mostly redundant because on a computer, you really only need a new-line character.
            – Nick Bedford
            Feb 9 '10 at 0:05






          • 3




            Long time ago, in the days of Teletypes and typewriters, output machines had carriages that moved left to write as characters were printed. One command, Carriage Return, moved the carriage back to the left. Another command, Linefeed, advanced the paper to the next line. These two commands could be executed independently so that the paper advanced mid-line (using Linefeed) or rewriting the current line (using Carriage Return). As a pair, they cause the printing to start at the left margin of the next line.
            – Thomas Matthews
            Feb 9 '10 at 0:26






          • 6




            Unix people, being impatient typists, decided that the computer should handle both Carriage Returns and Linefeeds, improving productivity by typing less characters. This new command was called Newline. On some output systems you could see the carriage move left and the paper advance for each Newline, including blank lines. The C language decided to make peace and let the OS provide translations (without ios::binary) or provide no translations (with ios::binary). The ios::out determines data direction (out from the computer).
            – Thomas Matthews
            Feb 9 '10 at 0:32












          • thank u mr. Thomas so can u give me one difference between using ios::binary and ios::out | ios::binary for opening a file r nt they identical?
            – Alan_AI
            Feb 9 '10 at 1:22














          5












          5








          5






          Opening a file with ios::binary controls how newline characters are handled. On Windows, they are expanded to CRLF pairs. That's it - it has no effect on how things like operator<< work.






          share|improve this answer












          Opening a file with ios::binary controls how newline characters are handled. On Windows, they are expanded to CRLF pairs. That's it - it has no effect on how things like operator<< work.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 8 '10 at 23:14







          anon



















          • but what are CLRF pairs?
            – Alan_AI
            Feb 8 '10 at 23:30






          • 2




            CLRF stands for carriage-return, line feed. These are the two bytes used to specify a new line in Windows text encoding. It's mostly redundant because on a computer, you really only need a new-line character.
            – Nick Bedford
            Feb 9 '10 at 0:05






          • 3




            Long time ago, in the days of Teletypes and typewriters, output machines had carriages that moved left to write as characters were printed. One command, Carriage Return, moved the carriage back to the left. Another command, Linefeed, advanced the paper to the next line. These two commands could be executed independently so that the paper advanced mid-line (using Linefeed) or rewriting the current line (using Carriage Return). As a pair, they cause the printing to start at the left margin of the next line.
            – Thomas Matthews
            Feb 9 '10 at 0:26






          • 6




            Unix people, being impatient typists, decided that the computer should handle both Carriage Returns and Linefeeds, improving productivity by typing less characters. This new command was called Newline. On some output systems you could see the carriage move left and the paper advance for each Newline, including blank lines. The C language decided to make peace and let the OS provide translations (without ios::binary) or provide no translations (with ios::binary). The ios::out determines data direction (out from the computer).
            – Thomas Matthews
            Feb 9 '10 at 0:32












          • thank u mr. Thomas so can u give me one difference between using ios::binary and ios::out | ios::binary for opening a file r nt they identical?
            – Alan_AI
            Feb 9 '10 at 1:22


















          • but what are CLRF pairs?
            – Alan_AI
            Feb 8 '10 at 23:30






          • 2




            CLRF stands for carriage-return, line feed. These are the two bytes used to specify a new line in Windows text encoding. It's mostly redundant because on a computer, you really only need a new-line character.
            – Nick Bedford
            Feb 9 '10 at 0:05






          • 3




            Long time ago, in the days of Teletypes and typewriters, output machines had carriages that moved left to write as characters were printed. One command, Carriage Return, moved the carriage back to the left. Another command, Linefeed, advanced the paper to the next line. These two commands could be executed independently so that the paper advanced mid-line (using Linefeed) or rewriting the current line (using Carriage Return). As a pair, they cause the printing to start at the left margin of the next line.
            – Thomas Matthews
            Feb 9 '10 at 0:26






          • 6




            Unix people, being impatient typists, decided that the computer should handle both Carriage Returns and Linefeeds, improving productivity by typing less characters. This new command was called Newline. On some output systems you could see the carriage move left and the paper advance for each Newline, including blank lines. The C language decided to make peace and let the OS provide translations (without ios::binary) or provide no translations (with ios::binary). The ios::out determines data direction (out from the computer).
            – Thomas Matthews
            Feb 9 '10 at 0:32












          • thank u mr. Thomas so can u give me one difference between using ios::binary and ios::out | ios::binary for opening a file r nt they identical?
            – Alan_AI
            Feb 9 '10 at 1:22
















          but what are CLRF pairs?
          – Alan_AI
          Feb 8 '10 at 23:30




          but what are CLRF pairs?
          – Alan_AI
          Feb 8 '10 at 23:30




          2




          2




          CLRF stands for carriage-return, line feed. These are the two bytes used to specify a new line in Windows text encoding. It's mostly redundant because on a computer, you really only need a new-line character.
          – Nick Bedford
          Feb 9 '10 at 0:05




          CLRF stands for carriage-return, line feed. These are the two bytes used to specify a new line in Windows text encoding. It's mostly redundant because on a computer, you really only need a new-line character.
          – Nick Bedford
          Feb 9 '10 at 0:05




          3




          3




          Long time ago, in the days of Teletypes and typewriters, output machines had carriages that moved left to write as characters were printed. One command, Carriage Return, moved the carriage back to the left. Another command, Linefeed, advanced the paper to the next line. These two commands could be executed independently so that the paper advanced mid-line (using Linefeed) or rewriting the current line (using Carriage Return). As a pair, they cause the printing to start at the left margin of the next line.
          – Thomas Matthews
          Feb 9 '10 at 0:26




          Long time ago, in the days of Teletypes and typewriters, output machines had carriages that moved left to write as characters were printed. One command, Carriage Return, moved the carriage back to the left. Another command, Linefeed, advanced the paper to the next line. These two commands could be executed independently so that the paper advanced mid-line (using Linefeed) or rewriting the current line (using Carriage Return). As a pair, they cause the printing to start at the left margin of the next line.
          – Thomas Matthews
          Feb 9 '10 at 0:26




          6




          6




          Unix people, being impatient typists, decided that the computer should handle both Carriage Returns and Linefeeds, improving productivity by typing less characters. This new command was called Newline. On some output systems you could see the carriage move left and the paper advance for each Newline, including blank lines. The C language decided to make peace and let the OS provide translations (without ios::binary) or provide no translations (with ios::binary). The ios::out determines data direction (out from the computer).
          – Thomas Matthews
          Feb 9 '10 at 0:32






          Unix people, being impatient typists, decided that the computer should handle both Carriage Returns and Linefeeds, improving productivity by typing less characters. This new command was called Newline. On some output systems you could see the carriage move left and the paper advance for each Newline, including blank lines. The C language decided to make peace and let the OS provide translations (without ios::binary) or provide no translations (with ios::binary). The ios::out determines data direction (out from the computer).
          – Thomas Matthews
          Feb 9 '10 at 0:32














          thank u mr. Thomas so can u give me one difference between using ios::binary and ios::out | ios::binary for opening a file r nt they identical?
          – Alan_AI
          Feb 9 '10 at 1:22




          thank u mr. Thomas so can u give me one difference between using ios::binary and ios::out | ios::binary for opening a file r nt they identical?
          – Alan_AI
          Feb 9 '10 at 1:22


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2225600%2fwhats-the-difference-between-opening-a-file-with-iosbinary-or-iosout-or-bot%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          To store a contact into the json file from server.js file using a class in NodeJS

          Redirect URL with Chrome Remote Debugging Android Devices

          Dieringhausen