Insert Text and Print Whole File || File Handling || C++

In this we are going to see a program of File Handling to insert any text and print the whole file 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()
{
    char line1[30], line2[30];
    fstream file;
    file.open("xyz.txt", ios::app | ios::in | ios::out);
    cout << "Enter a String: ";
    getline(cin, line1);
    file << line1;

    file.seekg(0, ios::beg);
    getline(file, line2);
    cout << line2;
    file.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()
{
    string line1, line2;
    fstream file;
    file.open("xyz.txt", ios::app | ios::in | ios::out);
    cout << "Enter a String: ";
    getline(cin, line1);
    file << line1;

    file.seekg(0, ios::beg);
    getline(file, line2);
    cout << line2;
    file.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