Skip to content

Commit db4addb

Browse files
author
Sumit tripathi
committed
File input and output example very easy code
1 parent 482deba commit db4addb

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

FileInputOutput.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
#include<iostream>
22
#include<fstream>
3+
/*
4+
The useful classes for working with files in C++ are:
5+
1. fstreambase
6+
2. ifstream --> derived from fstreambase
7+
3. ofstream --> derived from fstreambase
8+
*/
9+
10+
// In order to work with files in C++, you will have to open it. Primarily, there are two ways to open a file:
11+
// 1. Using the constructor
12+
// 2. Using the member function open() of the fstream class
313
using namespace std;
414
int main(){
15+
// string st = "This is a text to write in a file";
16+
string st2;
17+
// ofstream ot("FileInputOutput.txt"); // Write operation
18+
// ot << st<<endl; // Write the string to the file
19+
// ot.close(); // Closing the file after writing operation
20+
// Opening file using constructor and reading from it
21+
ifstream ik("FileInputOutput.txt"); // Read operation
22+
// ik >> st2; // Write the second string to the file --------> This show only the first word
23+
getline(ik, st2); // This will get the line from the file
24+
cout << st2 << endl; // Display the read string
25+
getline(ik, st2); // This will get the next line from the file
26+
cout << st2 << endl; // Display the read string
27+
getline(ik, st2); // This will get the next line from the file
28+
cout << st2 << endl; // Display the read string
529
return 0;
630
}

FileInputOutput.exe

46 KB
Binary file not shown.

FileInputOutput.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This is a text to write in a file
2+
Ram Ram Bhaiyaon
3+
Radhe Radhe

0 commit comments

Comments
 (0)