You are here:

C++/fstream variable as data member for both read and write in c++


can I use fstream variable as data member of a class for both read  and write in c++.
class myfile
......//data members of class

fstream m_file;//for both read and write.file should be in binary mode.
is m_file handel for both read and write in binary mode.Than how can we intialize it.
i.e i want to intialze fstream m_file("xyz",ios::in|ios::out|ios::binary) in the constructor or any memberfunction.for this how can I do .  

In a constructor you initialise data members and base classes using a constructor initialiser:

class myfile
   myfile( char const * pathname )
   : m_file( pathname, std::ios::in|std::ios::out|std::ios::binary )
   // ...
   // ...

// ...


A constructor initialiser is placed between the declaration of the constructor and the constructor body in the constructor definition. If not required then of course it need not be provided.

A constructor initialiser is started with a colon and is followed by a member initialiser list (member here means non-static data members and direct base classes). Each member or base class is initialised using the constructor (function call) syntax (i.e. member = initial_value is not allowed). Items in the member initialiser list are separated by commas.

If a data member or base class is not explicitly initialised in a constructor intialiser then it is initialised using the default constructor for the object's type. The order in which base classes and members are initialised is determined by their order in the class definition and _not_ the order in constructors. This ensures all constructors initialise objects in the same order.

You cannot use this syntax anywhere but a constructor. This makes sense - the members of an object can only logically be initialised when the object itself is initialised.

Note that you should consider what it means, if anything, to copy and assign objects of myfile. Should you provide an explicit copy constructor and assignment operator or should you prevent copying and assignment? As IO stream objects cannot be copied or assigned (copy and assignment are not defined for std::ios) you cannot rely on using the compiler generated defaults.

In other member functions you would have to use the std::fstream::open member function instead. You should of course check first to see if another file is open already and close it if it is:

class myfile

// ...

   void reopen( char const * pathname )
       if ( m_file.is_open() )
       } pathname
         , std::ios::in|std::ios::out|std::ios::binary

   // m_file state and open operation success
   // checking and reporting is not shown


I have not shown what the access to the reopen function has. If it is private then it could be used internally by any other member function that needs to reopen a file on the m_file stream. In this case it may be OK to leave the return type as void as the state of the m_file member could be checked as required. It would also be OK if errors are notified by throwing an exception. Otherwise you should consider returning a value to indicate whether the file operation was successful or not and/or including operations to allow users of objects of myfile to check the state of the object for themselves, of which the state of the m_file stream could form a part.

Please note that the code fragments are for demonstration purposes only and not intended for production use. Note that I assume a standard C++ IOStream library implementation with names in the std namespace (std::fstream, std::ios::in etc.). The code may contain typos or errors, and if so then I apologise.

Hope this helps.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.