I have a datafile.
#version 460 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}
And I am trying to read the contents of it with fread.
FILE *fshader;
char *fbuffer;
long fsize;
fshader = fopen("src/graphics/_fragment.shader", "r");
fseek(fshader, 0L, SEEK_END);
fsize = ftell(fshader);
rewind(fshader);
fbuffer = (char *)malloc(fsize + 1);
fread(fbuffer, 1, fsize, fshader);
fbuffer[fsize] = '\0';
But I don't understand how fread works. It keeps reading 6 characters more than it should.
#version 460 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}_to_te
fread
? – Zeitgeist\r\n
but translated back to\n
when read back. If you have 6 newlines, in text modeftell
no specified meaning and should only be used withfseek
, but is likely the byte-offset of the end of the file before the\r\n
are translated, so you'll put the nul 6 characters past the translated end of the text. – Sylvestersylviafread
returns the characters read after text translation so just capture the return value and use it for nul-terminating the string. – Sylvestersylviafbuffer[fsize] = '\0';
writes a NULL to the end of the buffer, but not to the end of the read data. You have to analyze the result offread()
. – Heindrick