How to read file content into istringstream in C++?


Here is a C++ program to read file content into isstringstream in C++.

Example

#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();
}

Output

hi tutorialspoint

Updated on: 30-Jul-2019

978 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements