Writing into Files in C++ || Basic Program || File Handling || C++
In this we are going to see a basic program of File Handling to write into files and close them 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");
    char line[20];
    getline(cin, line);
    f << line;
    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");
    string line;
    getline(cin, line);
    f << line;
    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