1#include <iostream>
2#include <ifstream>
3#include <string>
4
5using namespace std;
6
7ifstream file("file.txt");
8if (file.is_open())
9{
10 string line;
11 while (getline(file, line))
12 {
13 // note that the newline character is not included
14 // in the getline() function
15 cout << line << endl;
16 }
17}
1#include <iostream>
2#include <fstream>
3#include <string>
4using namespace std;
5int main(){
6 fstream newfile;
7 newfile.open("tpoint.txt",ios::out); // open a file to perform write operation using file object
8 if(newfile.is_open()) //checking whether the file is open
9 {
10 newfile<<"Tutorials point \n"; //inserting text
11 newfile.close(); //close the file object
12 }
13 newfile.open("tpoint.txt",ios::in); //open a file to perform read operation using file object
14 if (newfile.is_open()){ //checking whether the file is open
15 string tp;
16 while(getline(newfile, tp)){ //read data from file object and put it into string.
17 cout << tp << "\n"; //print the data of the string
18 }
19 newfile.close(); //close the file object.
20 }
21}