I am getting extra garbage at the end of the string returned by this function. Why?
char *read(char *fname)
{
FILE *file=fopen(fname,"r");
printf("Opening input file...\n");
if (file==NULL)
return NULL;
int length=0;
char *ret=new char[length];
while (!feof(file))
{
char ch=fgetc(file);
char *temp=new char[length+1];
for (int i=0; i<length; i++)
temp[i]=ret[i];
delete[]ret;
if (ch==EOF)
ch=0;
temp[length++]=ch;
ret=temp;
}
fclose(file);
return ret;
}