Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Gibberish returned when trying to return a char pointer

Posted on 2011-02-24
2
Medium Priority
?
437 Views
Last Modified: 2012-06-22
Hello,

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;

}

Open in new window


when I call for this function from main as so:
int main()
{
char *result;
result = GetID();
return 0;
}

Open in new window


"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!
0
Comment
Question by:JoeD77
2 Comments
 
LVL 10

Accepted Solution

by:
peetm earned 2000 total points
ID: 34972408
Aren't you effectively returning a pointer to a local variable.  Make finalbuff static as a quick sanity check.
0
 

Author Comment

by:JoeD77
ID: 34972445
ha that fixed it. Thanks!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question