将整个ASCII文件读入C ++ std :: string
这是在C++中将整个ASCII文件读入std::string的简单方法-
算法
Begin
Declare a file a.txt using file object f of ifstream type to perform read operation.
Declare a variable str of string type.
If(f)
Declare another variable ss of ostringstream type.
Call rdbuf() fuction to read data of file object.
Put the data of file object in ss.
Put the string of ss into the str string.
Print the value of str.
End.示例
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
using namespace std;
int main() {
ifstream f("a.txt"); //taking file as inputstream
string str;
if(f) {
ostringstream ss;
ss << f.rdbuf(); // reading data
str = ss.str();
}
cout<<str;
}输入项
a.txt data file containing the text “hi”
输出结果
hi
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短