Reading a file, unwanted chars at the end
Hi,
I`m reading chars to a char *buffer
Code:
ifstream datafile;
int length;
char * buffer;
try
{
datafile.open(filename, ios::binary );
if(!datafile.is_open())
return false;
char * buffer;
datafile.seekg (0, ios::end);
length = datafile.tellg();
datafile.seekg (0, ios::beg);
// allocate memory:
buffer = new char [length];
// read data as a block:
datafile.read(buffer,length);
// close file
datafile.close();
}
catch(...)
{
cout<<"\n\nError occurred while reading file\n\n";
}
the contents of the test.txt file
Code:
ksjdhafkjasnbdfkjhsdfjbsvdkjsdhgkjdshg
asdasd
asdasd
asdasd
g
g
and this is what the buffer contains
Code:
ksjdhafkjasnbdfkjhsdfjbsvdkjsdhgkjdshg
asdasd
asdasd
asdasd
g
gH=.;%D
How do I fix the code to get rid of that rubbish in the end?