Link to home
Start Free TrialLog in
Avatar of JoeD77
JoeD77

asked on

strcat causing crash when concatenating char

void SteamID()
{
	FILE * pFile;
  long lSize;
  char * buffer;
  size_t result;

  pFile = fopen ( "c:\\program files\\steam\\steam.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);
  if (result != lSize) {fputs ("Reading error",stderr); exit (3);}

  fclose (pFile);
  char * pch;
  char finalsid[256] = {' \0 '};
  int i = 0;

  pch = strstr (buffer,"for ");
  for(i = 0; i < 256; ++i)
  {

	if(pch[i] == '\n')
	{
		break;
	}
	else
	{
		//sprintf(finalsid, "%c", pch[i]);
		strcat(finalsid, (const char*)pch[i]);

		
		
	}
  }
  cout << finalsid;
}

Open in new window


Hello,
As you can see I'm using strstr to locate a substring within a file and reading that substring byte by byte until I reach a new line. And as I do that I'm trying to concatenate a new char array with just the data I want. Debug reports back "Access Violation"

Note: sprintf() removed the crash, and concatenated the string successfully. but it added a null byte to the end (or something of that sort) rendering the final result useless.

Any help would be appreciated

thanks!
Avatar of Shinesh Premrajan
Shinesh Premrajan
Flag of India image

Instead of the 256 in the for loop, it should be  the size of the pch.

Hope this helps
buffer and pch may not be null terminated
ASKER CERTIFIED SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you could use stat function to retrieve file size before fopen:

#include <sys/stat.h>

    struct stat fileinfo;
    int filesize;
    if (stat("c:\\program files\\steam\\steam.log", &fileinfo) != 0)
         {fputs ("File error",stderr); exit (1);}
   filesize = fileinfo.st_size;

    pFile = fopen(....


then it seems you were using c++ compiler. if so you should prefer new over malloc, std::ifstream over FILE/fopen/fread, std::iostream over fprintf or fputs and std::string over char buffers and c string handliing.

for example
 
   std::ifstream file("c:\\program files\\steam\\steam.log". std::ios_base::binary | std::ios_base::in);

would open the file

   std::string buffer(filesize, '\0');

woulld create a buffer of needed size and

   file.read(&buffer[0], filesize);

would read whole file.

with

    int n1 = 0;
    int n2 = buffer.find("\r\n", n1);

you would find the position of end-of-line pair of a text line  (when n2 != std::string::npos)
and with

   std::string strline = buffer.substr(n1, n2-n1);

you would extract the substring.

Sara



Avatar of JoeD77
JoeD77

ASKER

Thanks,

fixed with:

[code]
  for(i = 0; i < 256; ++i)
  {

      if(pch[i] == '\n')
      {
            
            strncpy((char*)finalbuff, (const char*)finalsid, strlen(finalsid) -1);
            //cout << finalbuff;
            
            break;

      }
      else
      {
            strncat((char *)finalsid, (const char*)pch + i, 1);
            
      }

  }
[/code]

now I have a new problem trying to return "finalbuff", it outputs gibberish in another function.

Will ask a new question for that.