我正在使用 OpenGL,我需要将 VertexShader.glsl 的内容放入 std::string
我看过相关的 StackOverflow 帖子,但我真的不知道如何将数据类型和内容匹配在一起以使其工作。
以 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;
}
我不知道之后发生了什么
std:: string content
之前每次用std::string都是这样
std::string name = "2bdkid";
最佳答案
template< class InputIt >
basic_string( InputIt first, InputIt last,
const Allocator& alloc = Allocator() );
哪些是:
Constructs the string with the contents of the range
[first, last)
.
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-constructedstd::istreambuf_iterator
is known as the end-of-stream iterator. When a validstd::istreambuf_iterator
reaches the end of the underlying stream, it becomes equal to the end-of-stream iterator.
content
由一对迭代器构成 - 第一个是我们对文件的单遍迭代器,第二个是流结束迭代器,充当哨兵。在本例中,string
构造函数引用的范围 [first, last)
是文件的全部内容。
关于c++ - 获取文件内容并将其放入 C++ 中的字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29737796/