File Handling: Write operation C++

Write a program in C++ to perform Write operation with in a file.

Here we are going to do two things:

i) Open the file.

ii) Ask user to write into the File and finally show the content of the file.

PROGRAM:

#include<iostream>
#include<stdio.h>
#include<fstream>
using namespace std;

int main()
{
    char c,fname[10];
    ofstream out;
    cout<<"Enter File name:";
    cin>>fname;
    out.open(fname);
    cout<<"Enter contents to store in file (Enter # at end):n";
    while((c=getchar())!='#')
         {
          cout<<c;
         }
    out.close();
    return 0;
}

 

OUTPUT:

file handling

Leave a Comment