file opening mode | Sololearn: Learn to code for FREE!

+1

file opening mode

#include <iostream> #include <fstream> using namespace std; int main () { ofstream MyFile1("test.txt"); MyFile1 << "This is awesome! \n"; MyFile1.close(); string line; ifstream MyFile("test.txt"); while ( getline (MyFile, line) ) { cout << line << '\n'; } MyFile.close(); } please explain each line of this program

10/25/2017 4:40:59 PM

Shivani Goyal

1 Answer

New Answer

+2

#include <iostream> #include <fstream> using namespace std; int main () { //Declare an output file stream object ofstream MyFile1("test.txt"); //Write a string into the stream with new line //character at the end MyFile1 << "This is awesome! \n"; //Close the stream MyFile1.close(); //Declare a buffer for file read string line; //Declare input file stream object ifstream MyFile("test.txt"); //print the content of the file in the console //repeat while there's still lines to read while ( getline (MyFile, line) ) { cout << line << '\n'; } //Close input file stream MyFile.close(); } Hth, cmiiw