-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path60_a_File_IO.cpp
More file actions
47 lines (34 loc) · 1.05 KB
/
60_a_File_IO.cpp
File metadata and controls
47 lines (34 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// File I/O in C++
// Using objects and classes
#include<iostream>
#include<fstream>
using namespace std;
/*
The useful classses for working with files in C++ are;
1. fstreambase
2. ifstream --> Derived from fstreambase
3. ofstream --> Derived from fstreambase
In order to work with files in C++, you will have to open it. There are two ways to open a file:
1. Using the constructor
2.
*/
int main()
{
string st = "Hello there...sample file...";
string st2;
// Opening files using Constructor and writing to it.
// Object "out" of class "ofstream"
ofstream out("sample.txt"); // Write Operation
out << st;
// ************************************************************************
// Opening files using Constructor and reading it.
ifstream in("sample2.txt"); // Read Operation
// in >> st2; --> only gives a single word
getline(in, st2);
cout << "\n" << st2 << endl;
getline(in, st2);
cout << st2 << endl;
getline(in, st2);
cout << st2 << "\n" << endl;
return 0;
}