-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathread-text-stream.cpp
More file actions
41 lines (35 loc) · 1.33 KB
/
read-text-stream.cpp
File metadata and controls
41 lines (35 loc) · 1.33 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
// Read text stream
#include <fstream>
#include <sstream>
#include <string>
std::string read_text_stream(std::istream& in)
{
std::ostringstream ss{};
ss << in.rdbuf();
return ss.str();
}
// Read entire content of a text stream into a string.
//
// Sometimes we want to read data from an input stream
// [item-by-item](/common-tasks/read-line-of-values.html) or
// [line-by-line](/common-tasks/read-line-by-line.html). But sometimes
// we want to read the entire stream's text content into a string
// variable, in one shot, without any parsing or formatting.
//
// On [9] we create a
// [`std::ostringstream`](cpp/io/basic_ostringstream). This is an output
// stream because we are reading *in* from the input stream (`in`), and
// writing what we read *out* to the string stream. We do this on [10],
// using input stream's [`rdbuf()`](cpp/io/basic_ifstream/rdbuf)
// function to get a pointer to its internal buffer. (We could also have
// used a [`std::streambuf`](cpp/io/basic_streambuf) object directly,
// without requiring a `std::istream` wrapper.)
//
// If we were able to successfully read the stream's contents, we can
// recover them as a string using `std::ostringstream`'s
// [`str()`](cpp/io/basic_ostringstream/str) function, as on [12].
int main()
{
std::ifstream in{"file.txt"};
std::string str = read_text_stream(in);
}