Menu

C++ TUTORIALS - C++ Files and Streams

C++ Files and Streams

ADVERTISEMENTS

Data TypeDescription
ofstreamThis data type represents the output file stream and is used to create files and to write information to files.
ifstreamThis data type represents the input file stream and is used to read information from files.
fstreamThis data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files.

ADVERTISEMENTS

Opening a File:

Mode FlagDescription
ios::appAppend mode. All output to that file to be appended to the end.
ios::ateOpen a file for output and move the read/write control to the end of the file.
ios::inOpen a file for reading.
ios::outOpen a file for writing.
ios::truncIf the file already exists, its contents will be truncated before opening the file.

ADVERTISEMENTS

Opening a File:

void open(const char *filename, ios::openmode mode);

ofstream outfile;
outfile.open("file.dat", ios::out | ios::trunc );

fstream  afile;
afile.open("file.dat", ios::out | ios::in );

Closing a File

void close();

Read & Write Example:

#include <fstream>
#include <iostream>
using namespace std;
 
int main ()
{
    
   char data[100];

   // open a file in write mode.
   ofstream outfile;
   outfile.open("afile.dat");

   cout << "Writing to the file" << endl;
   cout << "Enter your name: "; 
   cin.getline(data, 100);

   // write inputted data into the file.
   outfile << data << endl;

   cout << "Enter your age: "; 
   cin >> data;
   cin.ignore();
   
   // again write inputted data into the file.
   outfile << data << endl;

   // close the opened file.
   outfile.close();

   // open a file in read mode.
   ifstream infile; 
   infile.open("afile.dat"); 
 
   cout << "Reading from the file" << endl; 
   infile >> data; 

   // write the data at the screen.
   cout << data << endl;
   
   // again read the data from the file and display it.
   infile >> data; 
   cout << data << endl; 

   // close the opened file.
   infile.close();

   return 0;
}

$./a.out
Writing to the file
Enter your name: Zara
Enter your age: 9
Reading from the file
Zara
9

File Position Pointers:

// position to the nth byte of fileObject (assumes ios::beg)
fileObject.seekg( n );

// position n bytes forward in fileObject
fileObject.seekg( n, ios::cur );

// position n bytes back from end of fileObject
fileObject.seekg( n, ios::end );

// position at end of fileObject
fileObject.seekg( 0, ios::end );