C++ ofstream file access modes

Introduction

Mode Description
app Open the file for appending (adding to it).
ate Seek to end of file on opening it.
in Open the file for reading.
out Open the file for writing.
binary Open the file in binary mode.
truncDiscard contents if file exists
nocreate If file doesn't exist, open fails.
noreplaceIf file exists, open fails unless appending or seeking to end of file on opening.

Writes five names to a disk file in Binary Modes.

#include <fstream.h>
ofstream fp;/*from ww  w . j a  v  a  2  s  . c o m*/
void main()
{
   fp.open("NAMES.DAT", ios::out);  // Creates a new file.
   fp << "Michael Langston\n";
   fp << "Sally Redding\n";
   fp << "Jane Kirk\n";
   fp << "Stacy Wikert\n";
   fp << "Joe Hiquet\n";
   fp.close();   // Release the file.
   return;
}



PreviousNext

Related