将整个文件读入string

    技术2022-05-20  31

    IOStream著名专家Dietmar  Kuehl给过两个方法   

    1、使用流迭代器     这种方法很简单方便,但有一个缺点就是会将文件中的空格省掉     std::ifstream  in("some.file");        std::istreambuf_iterator<char>  beg(in),  end;        std::string  str(beg,  end);     2、使用字符串流      #include<sstream>      std::ifstream  in("some.file");        std::ostringstream  tmp;        tmp  <<  in.rdbuf();        std::string  str  =  tmp.str();   


    最新回复(0)