fstream


  • Till now, you created an object for either reading from or writing to the file.
  • You can also create a file for operating in more than one modes.
  • The advantage of using more than one mode is that you can read and write from the file without having to close and open the file each time in either read or write mode.
  • If you want to use the file for both input and output simultaneously, you declare the a stream to be an object of fstream class.
  • To associate the file with the stream, you have to open the file using the open() member function in the required modes.
  • In the open() function you include several mode bits to specify certain aspects of the file object you are opening. The options are given in the table.

  • E.g. open("data.dat",ios::in | ios::out | ios::app);
    Click here for example
  • This will open the file, data.dat for input or output or append modes. The in and out modes are included because you want to use the file for both reading and writing. The app mode is used to retain the previous contents of the file.
  • The vertical bar between the flags cause the bits representing these flags to be ORed together bitwise, so that several flags can apply simultaneously.
  • After opening the file, the necessary manipulations can be done. Finally, the file has to be closed using the close() member function. Click here for example

    Reading An ASCII String File<< Previous
    Next >> Positioning File Pointers

    Our aim is to provide information to the knowledge seekers. 


    comments powered by Disqus


Footer1