OneCompiler

ofstream-ifstream,Easy!!

 #include <iostream>
 #include <fstream>
 #include <string>
 using namespace std;
 
 int main() {
   string x;
   
   ofstream out;
   out.open("test.text");
   out<<"hello";
   out.close();
   //=======================
   ifstream in("test.text");
   in>>x;
   //=======================
   cout<<x;
   
   return 0;
 }
  • Result:

test.text (file)

hello

output

hello

1.We created an out object and asked it to create or open the text file test.text, write hello inside it, and then close it.

2.We created an in object that enters the test.text file and outputs the value of the first line in the variable x, then we print it on the screen.

    • Creat your code!!