-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathfile_read_all(binary).cpp
More file actions
35 lines (27 loc) · 865 Bytes
/
file_read_all(binary).cpp
File metadata and controls
35 lines (27 loc) · 865 Bytes
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
#include <fstream>
#include <string>
#include <iostream>
//https://stackoverflow.com/questions/18398167/how-to-copy-a-txt-file-to-a-char-array-in-c
//https://stackoverflow.com/questions/2602013/read-whole-ascii-file-into-c-stdstring
int main(){
std::ifstream in("xxx.txt");
// this only works if there is no whitespace
// in >> content;
// Method 1
std::string content1((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
/**
same as
std::string content1;
content1.assign((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
**/
// Method 2
std::stringstream ss;
ss << in.rdbuf();
std::string content2 = ss.str();
std::cout << content1 << std::endl;
std::cout << content2 << std::endl;
in.close();
return 0;
}