新手问题!我该如何将整个 ifstream 读入 stdlib 的字符串中?目前我正在为所有项目使用的方法浪费了很多时间:
string code;
ifstream input("~/myfile");
char c1=input.get();
while (c1!=EOF)
{
code+=c1;
len++;
c1=input.get();
}
顺便说一下,我更喜欢自己处理行和空格。
新手问题!我该如何将整个 ifstream 读入 stdlib 的字符串中?目前我正在为所有项目使用的方法浪费了很多时间:
string code;
ifstream input("~/myfile");
char c1=input.get();
while (c1!=EOF)
{
code+=c1;
len++;
c1=input.get();
}
顺便说一下,我更喜欢自己处理行和空格。
string load_file(const string& filename)
{
ifstream infile(filename.c_str(), ios::binary);
istreambuf_iterator<char> begin(infile), end;
return string(begin, end);
}
#include <string>
#include <iostream>
int main() {
std::string s;
std::getline(std::cin, s, '\0');
std::cout << s;
}
~