i'm having a weird problem with allocating memory in c++ i'm creating a buffer and read file content into it. problem is the allocating is incorrect and at the end of the printing there are weird chars... the content of the file is "Hello"... i'm sitting on it for hours... what can be the problem ? :(
void main()
{
FILE *fp;
char *buffer;
long file_size;
size_t result;
fp = fopen("input.txt","r");
if (fp == NULL) { fputs("File Error",stderr); exit(1); }
//get file size
fseek(fp, 0, SEEK_END);
file_size = ftell(fp);
rewind(fp);
//allocate memory to contain the whole file size
buffer = new char[file_size];
if (buffer == NULL) { fputs("Cannot allocate memory space",stderr); exit(2); }
//copy the file into the buffer
result = fread(buffer, 1, file_size, fp);
if (result != file_size) { fputs("Reading error",stderr); exit(3); }
cout<<buffer;
fclose(fp);
delete buffer;
}