title: Extra characters sometimes getting tacked on to the end of a character array containing characters that have been extracted from a file
body: <p>I'm having trouble reading in shader files for OpenGL. Sometimes the files are read correctly and the shaders can be correctly initialized. Other times, they are not read correctly.</p>
  
  <p>The way I read these files (containing the code for the shaders) is simple. I extract each character from the file into an array. That array then effectively becomes the code for the shader and can be passed to opengl for use.</p>
  
  <p>The problem is, sometimes some extra characters are tacked onto the end of the array when I read shader file</p>
  
  <p>So instead of this:</p>
  
  <pre><code>#version 410 core
  
  in vec4 fragmentColor;
  out vec4 color;
  
  void main(){
      color = fragmentColor;
  }
  </code></pre>
  
  <p>I get this:</p>
  
  <pre><code>#version 410 core
  
  in vec4 fragmentColor;
  out vec4 color;
  
  void main(){
      color = fragmentColor;
  }\377
  </code></pre>
  
  <p>And I end up with a black screen.</p>
  
  <p>Again, this doesn't happen all the time, happening seemingly at random.</p>
  
  <p>This is my function that actually reads the shader files and extracts the characters into an array. <em>(Heavy with comments since I'm assuming posting long code on here is a sin. But the function itself really isn't all that complicated)</em></p>
  
  <pre><code>    const GLchar ** ReadShaders(std::string vertex, std::string fragment){
  
  
      char tempVert[10000];
      char tempFrag[10000];
  
      //Open shader files
      std::ifstream vert(vertex);
      std::ifstream frag(fragment);
  
      //Don't skip white lines
      vert &gt;&gt; std::noskipws;
      frag &gt;&gt; std::noskipws;
  
      //Read in vertex shader file by extracting each character into "tempVert"
      int vCount = 0;
  
      while( !vert.eof() ){
  
          vert &gt;&gt; tempVert[vCount];
  
          //Break if there are no more characters to read (I know this is redundant, will fix later)
          if(vert.eof()){
              break;
          }
          else{
              vCount++;
          }
  
      }
  
      //Debug
      std::cout &lt;&lt; vCount &lt;&lt; std::endl;
  
      //Create new char array the size of the number of characters extracted from shader file
      char * tempVertexSource = new (std::nothrow) GLchar[vCount];
  
      //Copy extracted characters into new "tempVertexSource" array
      for(int i = 0; i &lt; vCount; i++){
  
          tempVertexSource[i] = tempVert[i];
  
      }
  
      //Debug
      std::cout &lt;&lt; tempVertexSource &lt;&lt; std::endl;
  
      //Read in fragment shader file by extracting each character into "tempFrag"
      int fCount = 0;
  
      while( !frag.eof() ){
  
          frag &gt;&gt; tempFrag[fCount];
  
          //Break if there are no more characters to read (I know this is redundant, will fix later)
          if(frag.eof()){
              break;
          }
          else{
              fCount++;
          }
      }
  
      //Debug
      std::cout &lt;&lt; fCount &lt;&lt; std::endl;
  
      //Create new char array the size of the number of characters extracted from shader file
      GLchar * tempFragmentSource = new (std::nothrow) GLchar[fCount];
  
      //Copy extracted characters into new "tempFragmentSource" array
      for(int i = 0; i &lt; fCount; i++){
          tempFragmentSource[i] = tempFrag[i];
      }
  
      //Debug
      std::cout &lt;&lt; tempFragmentSource &lt;&lt; std::endl;
  
      //Create an array to hold both shader arrays (for return)
      const GLchar ** returnShaders = new (std::nothrow) const GLchar*[2];
  
      //Assign shaders
      returnShaders[0] = tempVertexSource;
      returnShaders[1] = tempFragmentSource;
  
      //Close files
      vert.close();
      frag.close();
  
      //Return
      return returnShaders;
  
  
  }
  </code></pre>
  
  <p>Now, in my function, the number of characters that are extracted from a file is counted. So, this next part is where I'm confused (other then the fact that sometimes the error happens, and sometimes it doesn't). </p>
  
  <p>When outputting (checking) the number of characters that have been extracted from a file I'll get this for a correct run:</p>
  
  <pre><code>Number of characters: 100
  
  #version 410 core
  
  in vec4 fragmentColor;
  out vec4 color;
  
  void main(){
      color = fragmentColor;
  }
  </code></pre>
  
  <p>However, I'll get the same number of characters when an incorrect reading of the file happens as well:</p>
  
  <pre><code>Number of characters: 100
  
  #version 410 core
  
  in vec4 fragmentColor;
  out vec4 color;
  
  void main(){
      color = fragmentColor;
  }\377
  </code></pre>
  
  <p>This doesn't make sense, since:</p>
  
  <p>1) The random extra characters seemingly come out of nowhere and at random</p>
  
  <p>2) The number of characters that have been extracted from the file are used for the size of the new array that will contain those characters, so this shouldn't even be possibly without those extra characters all being a part of the last character in the array.</p>
  
  <p>So, for example: Going off of what I know, for this particular shader file that contains 100 characters, if I hardcode 99 for the size of the char array and the number of characters it will copy into this new array, sure I don't get any of the random characters at the end at times, but it also deletes the "}" character. </p>
  
  <p>Making it:</p>
  
  <pre><code>#version 410 core
  
  in vec4 fragmentColor;
  out vec4 color;
  
  void main(){
      color = fragmentColor;
  </code></pre>
  
  <p>I'm very confused, Thanks for any help</p>