#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
int main()
{
const char* vertexPath = " "; // 加载文件名
std::string vertexCode;
std::ifstream vShaderFile;
// 保证ifstream对象可以抛出异常:
vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
try
{
// 打开文件
vShaderFile.open(vertexPath);
std::stringstream vShaderStream;
// 读取文件的缓冲内容到数据流中
vShaderStream << vShaderFile.rdbuf();
// 关闭文件处理器
vShaderFile.close();
// 转换数据流到string
vertexCode = vShaderStream.str();
}
catch(std::ifstream::failure e)
{
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
}
const char* vShaderCode = vertexCode.c_str(); // string转换成const char*
[...]
return 0;
}
读文件流转换为string,再转换为char*(异常处理)
最新推荐文章于 2023-02-16 22:58:23 发布