Answer:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream out;
out.open("Hello.txt");
out << "Hello, World!"<< endl;
out.close();
ifstream in;
in.open("Hello.txt");
while(in.good()){
string line;
in >> line;
cout << line << endl;
}
in.close();
return 0;
}
Step-by-step explanation:
Step 1:
Add all your preprocessor files directive, #include is known as the preprocessor is used to load files needed to run the code.
#include<iostream>: it provides basic input and output services for C++ programs.
#include<fstream>:used to open a file for writing and reading
using namespace std; means you'll be using namespace std means that you are going to use classes or functions (if any) from "std" namespace
Step 2:
ofstream out; his is used to create files and write data to the file
out.open("Hello.txt"); a new file "HELLO.txt" has been created
out << "Hello, World!"<< endl; "Hello World!" has now been writen to the file
out.close(); The file is now closed
This second step opens a new file and writes hello world to the file then closes it.
Step 3:
ifstream in; ifstream in c++ is a stream class which stands for input file stream a is used for reading data from file.
in.open("Hello.txt"); Reopens the file without recreating it. This is a very important step because if not done properly, you might discard the file you previously created and recreate it.
while(in.good()){
in.good() is true if the previous read operation succeeded without EOF being encountered
string line; It declares a string variable line
in >> line; reads the data into the line variable
cout << line << endl; Prints out the line
reopens the file and read the message into a string variable "line". prints out line then exits out of the file.