Reading a simple text file in c++ display invalid characters at the end of buffer,
string filecontent="";
ifstream reader(fileName);
reader.seekg (0, reader.end);``
int length = reader.tellg();
reader.seekg (0, reader.beg);
char *buffer=new char[length];
reader.read(buffer,length);
filecontent=buffer;
reader.close();
cout<<"File Contents"<<std::endl;
cout<<filecontent;
delete buffer;
return false;
but when i specify buffer length incremented by one ie
char *buffer=new char[length+1];
reader.read(buffer,length+1);
it works fine without invalid characters i want to know what is the reason behind this?