JoeD77
asked on
Gibberish returned when trying to return a char pointer
Hello,
This code:
when I call for this function from main as so:
"result" is all gibberish, even though "finalbuff" that is being returned isn't.
So basically I need to know how to return a char pointer properly
Thanks!
This code:
char *GetID()
{
FILE * pFile;
long lSize;
char * buffer;
size_t result;
pFile = fopen ( "c:\\program files\\example\\example.log" , "rb" );
if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
result = fread (buffer,1,lSize,pFile);
// buffer[lSize] = { '\n' };
if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
fclose (pFile);
char * pch;
// char * nonNull;
char finalsid[256] = {' \0 '};
int i = 0;
char finalbuff[256] = { '\0' };
if(strstr(buffer, "0:1:") == NULL && strstr(buffer, "0:0:") == NULL)
{
puts("Not Installed");
exit(0);
}
if(strstr (buffer,"0:1:") == NULL)
{
pch = strstr (buffer,"0:0:");
}
else{
if(strstr(buffer, "0:0:") == NULL)
{
pch = strstr (buffer,"0:1:");
}
}
for(i = 0; i < 256; ++i)
{
if(pch[i] == '\n')
{
strncpy((char*)finalbuff, (const char*)finalsid, strlen(finalsid) -1);
break;
}
else
{
strncat((char *)finalsid, (const char*)pch + i, 1);
}
}
return finalbuff;
}
when I call for this function from main as so:
int main()
{
char *result;
result = GetID();
return 0;
}
"result" is all gibberish, even though "finalbuff" that is being returned isn't.
So basically I need to know how to return a char pointer properly
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER