Reputation: 53
I've just started learning OpenGL, but I've run into a slight problem. I wrote a custom function to read in the shaders from a file, and here it is.
char* ReadShaderSource(const char* path)
{
std::ifstream is(path, std::ios::in);
if (!is)
return NULL;
is.seekg(0, std::ios::end);
long size = is.tellg();
is.seekg(0, std::ios::beg);
char *src = new char[size + 1];
is.read(src, size);
src[size] = '\0';
return src;
}
As you can see, after moving the pointer to end of file, I use is.tellg() to get the size of the file. However, in a simple shader, it displays the count more than what it actually is. #version 150
in vec4 pos;
void main()
{
gl_Position = pos;
}
Here, the size should be 66 (I counted manually, I might be off by 1 or 2 chars). However, it displays it as 73. I'm GUESSING the fact that \n\r is read as \n might have something to do with the wrong character count, but I'm not sure.
Either way, my question is, why the hell am I getting the wrong count, and how can I get it correctly.
Note: I am aware of other methods to count the size of file, or better yet, directly read the contents in a std::string and then convert them to char* , but I'd like to know what the problem in this code is, and how I can resolve it.
Upvotes: 1
Views: 806
Reputation:
You are not counting the 6 carriage return characters and one tab (which adds up to the 7 chars you are missing), but tellg()
does.
Upvotes: 1