Interview Questions   Tutorials   Discussions   Programs   Videos   

C++ - How to open binary files in c++?

asked marvit August 18, 2014 11:54 AM  

How to open binary files in c++?


1 Answers

answered By bhjahnavi   0  
A file stream object can be opened in one of two ways. First, you can supply a file name along with an i/o mode parameter to the constructor when declaring an object:
    ifstream myFile ("data.bin", ios::in | ios::binary);
Alternatively, after a file stream object has been declared, you can call its open method:
    ofstream myFile; ("data2.bin", ios::out | ios::binary);
Either approach will work with an ifstream, an ofstream, or an fstream object.
Normally, when manipulating text files, one omits the second parameter (the i/o mode parameter). However, in order to manipulate binary files, you should always specify the i/o mode, including ios::binary as one of the mode flags. For read/write access to a file, use an fstream:
    fstream myFile; ("data3.bin", ios::in | ios::out | ios::binary);
   add comment

Your answer

Join with account you already have



 Write A Tutorials
Online-Classroom Classes

  1 person following this question

  Question tags

c++ × 1

Asked 3 years and 5 months ago ago
Number of Views -274
Number of Answers -1
Last updated
3 years and 3 months ago ago

Ready to start your tutorial with us? That's great! Send us an email and we will get back to you as soon as possible!