Solved

Gibberish returned when trying to return a char pointer

Posted on 2011-02-24
2
426 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 500 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

Gigs: Get Your Project Delivered by an Expert

Select from 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

Suggested Solutions

Title # Comments Views Activity
C++ standard library based binary archive format 6 98
Unresolved External Symbols 3 80
How to gracefully close the c++ 11 thread? 3 94
c++ syntax question 9 47
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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now