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;
}
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; <em>his is used to create files and write data to the file</em>
out.open("Hello.txt"); <em> a new file "HELLO.txt" has been created</em>
out << "Hello, World!"<< endl; "<em>Hello World!" has now been writen to the file</em>
out.close(); <em>The file is now closed</em>
<em>This second step opens a new file and writes hello world to the file then closes it.</em>
Step 3:
<em>ifstream in; ifstream in c++ is a stream class which stands for input file stream a is used for reading data from file.</em>
in.open("Hello.txt"); <em>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.</em>
while(in.good()){
<em> in.good() is true if the previous read operation succeeded without EOF being encountered</em>
string line; <em>It declares a string variable line</em>
in >> line; <em>reads the data into the line variable</em>
cout << line << endl; <em>Prints out the line</em>
<em>reopens the file and read the message into a string variable "line". prints out line then exits out of the file.</em>