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();
}

#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

1 thought on “C++ program file handling create an empty text file and assign some values”

  1. Pingback: C++ projects for beginners with source code - Codeboks

Leave a Comment

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