ifstream:
参考博客: ifstream 的使用方法介绍 一_龙贝尔莱利的博客-优快云博客_ifstream
ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就是内存空间;
代码框架:
struct ShaderProgramSource
{
std::string VertexSource;
std::string FragmentSource;
}
static ShaderProgramSource ParseShader(const std::string& filepath)
{
std:ifstream stream(filepath);
//枚举关键字
enum class ShaderType{
};
//搜索关键字
while(getline(stream,line)
{
在stream文件找 line
}
//关键字匹配
}
源码
struct ShaderProgramSource
{
std::string VertexSource;
std::string FragmentSource;
};
static ShaderProgramSource ParseShader(const std::string& filepath)
{
std::ifstream stream(filepath); //从硬盘到内存
enum class ShaderType
{
NONE=-1,VERTEX=0,FRAGMENT=1
};
std::stringstream ss[2];
ShaderType type=ShaderType::NONE;
std::string line;
while (getline(stream, line))
{
if (line.find("#shader") != std::string::npos)
{
if (line.find("vertex") != std::string::npos)
type = ShaderType::VERTEX;
else if (line.find("fragment") != std::string::npos)
type = ShaderType::FRAGMENT;
}
else
{
ss[(int)type] << line << "\n";
}
}
return { ss[0].str(),ss[1].str() };
}
ENUM
C++ enum枚举类型_一叶执念的博客-优快云博客_c++ emun
枚举是个好东西,它和结构体一样,都可以定义一种数据类型,但是枚举有一个更优点,就是它可以默认指定定义的初始值。
有一个项目,涉及到CPU卡,卡片有错误、上电、DF01目录等状态,如果单独定义这些变量,要使用很多变量,这里有一个规律,即:CPU卡不能同时为这几种状态中的几个,只能为其中的一个,这样,我们可以定义一个枚举类型变量,例如:
//标识ic的几种状态
typedef enum
{
ICC_ERROR,
ICC_NO_EXIST,
ICC_EXIST,
ICC_POWER_OFF,
ICC_POWER_ON,
ICC_IN_DIR_1001
} t_IccStatus;
t_IccStatus getIccStatus;
在程序中,使用这个枚举变量既能标识标识在整个工程中CPU卡的状态,简洁,明了,并且节省了很多全局变量的使用。
返回值类型为枚举:
一般情况下,一个函数都有一个返回值,在一个工程中,如果在每个出错点,都定义一个特殊数字的返回值,这样,在main中,返回的就是唯一值,这样,可以很快的定位出出错点。
如果和上面的枚举配合起来,将函数的返回值定义为枚举类型,在定义枚举时,使用英文单词来实现各种不同返回值的定义,这样,在调试的时候,能够知名达意,很快的找到出错点,达到事半功倍的效果。