ch2-FillString (Read file into a string)
Chapter_2 Exercise_2-2 Scopy | Fillvector Exercise_2-3 |
FillString.cpp TCP1, p. 112 download
// Read an entire file into a single string
#include <iostream>
#include <string>
#include <fstream>
using std::cout;
using std::string;
using std::ifstream;
using std::ofstream;
int main()
{
ifstream in("FillString.cpp");
string s, line;
while(getline(in, line)) // getline() removes '\n'
{s += line + '\n';} // char '\n' is converted into string "\n"
cout << s;
}
/*
g++ FillString.cpp -o FillString
./FillString
*/
Chapter_2 Exercise_2-2 Scopy | BACK_TO_TOP | Fillvector Exercise_2-3 |
Comments
Post a Comment