I am returning a char pointer from a function. But the caller is unable to see the string.
char* getFileContent(char* file)
{
FILE* fp = fopen("console.txt", "r");
fseek(fp, 0L, SEEK_END);
size_t sz = ftell(fp);
fseek(fp, 0L, SEEK_SET);
char* message = (char*)malloc(sz+1);
char buf[sz+1];
size_t len = 0;
if (fp != NULL)
{
len = fread(buf, sizeof(char), sz, fp);
}
printf("MALLOC SIZE:%d FILE SIZE:%d", sz, len);
strcpy(message,buf); //Modified code. This line fixed the code
message[++len] = '\0';
//printf("MESSAGE:%s", message);
return(message);
}
This is the caller. Output is empty.
int main(int argc, char **argv, char **env)
{
char* msg = getFileContent(imagefile);
if(msg != NULL)
printf("Output:%s \n", msg);
free(msg);
return 0;
}
Please help.