file opening mode | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

25th Oct 2017, 4:40 PM
Shivani Goyal
1 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
25th Oct 2017, 5:42 PM
Ipang