-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathread_all_file_contents.cpp
More file actions
55 lines (43 loc) · 1.28 KB
/
read_all_file_contents.cpp
File metadata and controls
55 lines (43 loc) · 1.28 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
48
49
50
51
52
53
54
55
/*******************************************************************************
*
* Program: Read All File Contents Into A String
*
* Description: Example of how to read all the contents of a file into a string
* with C++.
*
* YouTube Lesson: https://www.youtube.com/watch?v=q97E8rOFWSU
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main(void)
{
// create an ifstream object to open and access the file
ifstream file;
// open the file
file.open("file.txt");
// exit with an error message and status if the file fails to open
if (file.fail())
{
cout << "File failed to open." << endl;
return 1;
}
// create a stringstream object to help read the file contents
stringstream buffer;
// reads entire contents of the file into the stringstream buffer
buffer << file.rdbuf();
// create the string to store the file contents
string file_contents;
// get the string from the stringstream object, store it into the
// file_contents string
file_contents = buffer.str();
// output the file contents
cout << file_contents;
// close the file
file.close();
return 0;
}