-
Notifications
You must be signed in to change notification settings - Fork 0
/
streams.cpp
50 lines (40 loc) · 1.08 KB
/
streams.cpp
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
48
49
50
/*
Fun with streams
1. Reading whole file into a string
2. Reading whole file into std::cin
3. Writing into std::cout via ostream iterator
*/
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <iterator>
int main()
{
// open file
std::ifstream ifs("C:\\Users\\KK\\Desktop\\test.txt");
// read the whole file into std::ostringstream and print
std::ostringstream oss;
oss << ifs.rdbuf();
std::cout << oss.str() << std::endl;
// rewind the file and reset flags
ifs.seekg(0);
ifs.clear();
// read the whole file into std::cin
std::cin.rdbuf(ifs.rdbuf());
// iterate through data and print
std::istream_iterator<std::string> it(std::cin);
std::istream_iterator<std::string> eos;
for (; it != eos; ++it)
std::cout << *it << std::endl;
// output writing into ostream iterator
std::ostream_iterator<std::string> it(std::cout, ",");
std::ostream_iterator<std::string> it2(std::cout);
// ostream specifics
it = "one"; // same
*it = "two"; // same
*it++ = "three"; // same
*++it = "four"; // same
it2 = "five";
std::cout << std::endl;
}