如何将文件内容读入 C++ 中的 istringstream?


下面是 C++ 程序,用来将文件内容读入 C++ 中的 istringstream。

示例

#include <fstream>
#include <sstream>
#include<iostream>
using namespace std;
int main() {
   ifstream is("a.txt", ios::binary );
   // get length of file:
   is.seekg (0, std::ios::end);
   long length = is.tellg();
   is.seekg (0, std::ios::beg);
   // allocate memory:
   char *buffer = new char [length];
   // read data as a block:
   is.read (buffer,length);
   // create string stream of memory contents
   istringstream iss( string( buffer ) );
   cout<<buffer;
   // delete temporary buffer
   delete [] buffer;
   // close filestream
   is.close();
}

输出

hi tutorialspoint

更新日期:2019 年 7 月 30 日

1000+ 浏览量

开启你的职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.