home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 33,236 of 33,346   
   Joe Bentley to All   
   Is the binary open mode required?   
   08 Oct 13 04:51:48   
   
   From: joe.foxhound@googlemail.com   
      
   I don't see any difference between using the binary open mode or not when   
   writing text or binary files?  Is there a difference or am I missing   
   something?   
      
   My example:   
      
   // Do you have to open in binary?   
      
   #include    
   #include    
   #include    
   using namespace std;   
      
   int main()   
   {   
       ofstream fout_t1("file1t.txt");   
       ofstream fout_b1("file1b.txt",ios_base::binary);   
       ofstream fout_t2("file2t.txt");   
       ofstream fout_b2("file2b.txt",ios_base::binary);   
      
       int i = 5;   
       double d = 9.8;   
       char x = 'Z';   
       string s = "Have a nice day";   
      
       // write to text file   
       fout_t1 << i << ' ' << d << ' ' << x << ' ' << s;   
       // write to binary file   
       fout_b1 << i << ' ' << d << ' ' << x << ' ' << s;   
      
       // write to text file   
       fout_t2.write(reinterpret_cast(&i), sizeof(i));   
       fout_t2.write(reinterpret_cast(&d), sizeof(d));   
       fout_t2.write(reinterpret_cast(&x), sizeof(x));   
       fout_t2.write(reinterpret_cast(&s), sizeof(s));   
      
       // write to binary file   
       fout_b2.write(reinterpret_cast(&i), sizeof(i));   
       fout_b2.write(reinterpret_cast(&d), sizeof(d));   
       fout_b2.write(reinterpret_cast(&x), sizeof(x));   
       fout_b2.write(reinterpret_cast(&s), sizeof(s));   
      
       return 0;   
   }   
      
      
   Thanks,   
   Joe   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca