8

I'm using OpenGL and I need the contents of VertexShader.glsl to be put into a std::string

I've looked at the related StackOverflow posts about this but I don't really know how to match together data types and stuff to make it work.

Take for example this from Read file-contents into a string in C++

#include <fstream>
#include <string>

int main(int argc, char** argv)
{

  std::ifstream ifs("myfile.txt");
  std::string content( (std::istreambuf_iterator<char>(ifs) ),
                       (std::istreambuf_iterator<char>()    ) );

  return 0;
}

I have no clue what's happening after

std:: string content

Every time I have used std::string before has been like

std::string name = "2bdkid";
Barry
  • 247,587
  • 26
  • 487
  • 819
Brady Dean
  • 2,150
  • 2
  • 19
  • 38

1 Answers1

9

It's constructor #6 here:

template< class InputIt >
basic_string( InputIt first, InputIt last,
              const Allocator& alloc = Allocator() );

Which does:

Constructs the string with the contents of the range [first, last).

istreambuf_iterator is:

a single-pass input iterator that reads successive characters from the std::basic_streambuf (ifs in this example) object for which it was constructed... The default-constructed std::istreambuf_iterator is known as the end-of-stream iterator. When a valid std::istreambuf_iterator reaches the end of the underlying stream, it becomes equal to the end-of-stream iterator.

content is constructed from an iterator pair - the first one of which is our single-pass iterator into the file, and the second of which is the end-of-stream iterator that acts as a sentinel. The range [first, last) that the string constructor refers to is, in this case, the full contents of the file.

Barry
  • 247,587
  • 26
  • 487
  • 819