C++ program file handling create an empty text file and assign some values

C++ program file handling Reading and Writing Some Values

C++ program file handling create an empty text file and assign some values

Objects:

Write a program in C++ that will: 

  1. Create an empty text file example.txt using ofstream class on default location.
  2. Assign some values to example.txt file.
  3. Open example.txt file and print its data on C++ console window.

Code:

#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>
using namespace std;
void main()
{
       ofstream task1;
       string data;
       task1.open(“ahmer.txt”);
       cout<<“Enter any text here.\n”;
       getline(cin,data);
       task1<<data;
       cout<<“\n\nData save in file…”;
       cout<<“\nPrint file data in your screen.\n”;
       ifstream task2;
       task2.open(“ahmer.txt”);
       task2>>data;
       cout<<data;

       getch();
}

Output:

C++ program file handling create an empty text file and assign some values

M.A Siddiqui

Hi, I am M.A Siddiqui. I'm a Computer Science Student, In this website, I'm teaching you Multiple languages and provides Projects and other Subjects Notes.

You may also like...

1 Response

  1. June 14, 2020

    […] C++ program file handling create an empty text file and assign some values […]

Leave a Reply

Your email address will not be published. Required fields are marked *