获取文件内容并将其放入 C++ 中的字符串中
Take file contents and put it into a string in C++
我正在使用 OpenGL,我需要将 VertexShader.glsl 的内容放入 std::string
我查看了有关此问题的相关 Whosebug 帖子,但我真的不知道如何将数据类型和内容匹配在一起以使其发挥作用。
以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-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
由一对迭代器构成——第一个是我们进入文件的单遍迭代器,第二个是充当哨兵的流结束迭代器.在本例中,string
构造函数引用的范围 [first, last)
是文件的全部内容。
我正在使用 OpenGL,我需要将 VertexShader.glsl 的内容放入 std::string
我查看了有关此问题的相关 Whosebug 帖子,但我真的不知道如何将数据类型和内容匹配在一起以使其发挥作用。
以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)
是文件的全部内容。