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

C++Server Side ProgrammingProgramming

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
raja
Published on 18-Jun-2019 11:57:19
Advertisements