Creating and Opening Files in c++




This video tutorial explains how to create and open files in c++.

You are gonna learn how to use classes such as fstream, ifstream, ofstream, how to create an object of fstream class, how to associate a file with a stream, which file opening modes are available,how to open a file using a specific file opening mode, how to check whether a file has opened successfully or not, how to close a file in detail with example.

source code for this tutorial

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    fstream file;

    file.open("anil.txt",ios :: in | ios :: out | ios::trunc);

    if(!file.is_open()){
        cout << "error while opening the file";

    }else{
    //read or write
    cout << "file opened successfully";
    file.close();
    }

    return 0;
}