Problem when read txt file C++

Hi guys.
Here my code:

long len;
char* buf = NULL;
FILE* fp = NULL;
fp = fopen( "File.txt", "rb" );
if (fseek( fp, 0, SEEK_END ) != 0)
{
fclose( fp );
}
len = ftell( fp );
rewind( fp );
buf = (char*)malloc( len );
if (!fread(buf,len,1,fp)) return;
fclose( fp );
printf(buf);

My File.txt:
Testing
Test
But i'm getting a problem..
It returns all strings of my text, but at end the text, appear symbols like:
Testing
Test.¶­♠ // Like it

or
Testing
Test.log// Like it also

I should use '\0' to clean empty bytes but i haven't any idea of how can i use here..
Thanks!
change buf = (char*)malloc( len ); to buf = (char*)malloc( len + 1); // Note: +1
and add buf[len] = 0; after if (!fread(buf,len,1,fp)) return;

Please use code tags: [code]Your code[/code]
Read this: http://www.cplusplus.com/articles/z13hAqkS/
Topic archived. No new replies allowed.