Open a File and Close it || Basic Program || File Handling || C++
In this we are going to see a basic program of File Handling to open a file and close it in C++ Programming Language. 



The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include <fstream.h>
#include<conio.h>

void main()
{
    ofstream f;
    f.open("xyz.txt");
    f << "Inserted directly";
    if (f)
    {
        cout << "Successfully inserted into the file";
    }
    else
    {
        cout << "Nope";
    }
    f.close();
    getch();
}

//.......Coded by SAHIL SHAIKH


The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ofstream f;
    f.open("xyz.txt");
    f << "Inserted directly";
    if (f.is_open())
    {
        cout << "Successfully inserted into the file";
    }
    else
    {
        cout << "Nope";
    }
    f.close();
}

//.......Coded by SAHIL SHAIKH

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post