Original Post
I need to read a text file(GLSL shader) and pass it to glCompileShader but my current file reading method doesn't work as it returns a std::string.And all my attempts of converting it to a char array failed.It usually doesn't read new lines and doesn't compile etc. Here is the c++ code : I can go with something like : But I'm wondering if it could be done with c++ streams.
std::string STUtil::fileToString(const std::string& filename)
{
//open file for reading
std::fstream file(filename.c_str(),std::ios::in);
//throw exception if we fail
if(!file)
throw std::ios::failure("file not found");
//read all the file
std::stringstream buffer;
buffer << file.rdbuf();
std::string content(buffer.str());
//return the string
return content;
}
FILE *fp;
char *content = NULL;
int count=0;
fp = fopen(filename.c_str(),"rt");
if (fp != NULL)
{
fseek(fp, 0, SEEK_END);
count = ftell(fp);
rewind(fp);
if (count > 0)
{
content = new char [count+1];
count = fread(content,sizeof(char),count,fp);
content[count] = '\0';
}
fclose(fp);
}
return content;