Reputation: 4137
noob question! How can i read 'a whole ifstream' into a stdlib 'string'? The current way i'm using for all my projects right now wastes much time i think:
string code;
ifstream input("~/myfile");
char c1=input.get();
while (c1!=EOF)
{
code+=c1;
len++;
c1=input.get();
}
BTW i prefer to do line and whitespace management myself.
Upvotes: 4
Views: 1097
Reputation: 168626
#include <string>
#include <iostream>
int main() {
std::string s;
std::getline(std::cin, s, '\0');
std::cout << s;
}
~
Upvotes: 4
Reputation: 52083
string load_file(const string& filename)
{
ifstream infile(filename.c_str(), ios::binary);
istreambuf_iterator<char> begin(infile), end;
return string(begin, end);
}
Upvotes: 8