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

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 729
  • Last Modified:

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')
		//sprintf(finalsid, "%c", pch[i]);
		strcat(finalsid, (const char*)pch[i]);

  cout << finalsid;

Open in new window

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

2 Solutions
Shinesh PremrajanCommented:
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
The following discussion assumes that you understand pointers and arrays. For review, here is a tutorial:

malloc should make room for a terminating null byte.

After the fread, now ensure termination by adding the 0.

The loop is char oriented; but strcat is string oriented, so no loop needed.

pch is pointing to "for ...\n...."
To terminate the pch string, you could overwrite the newline. One way to do this is to define pch2 to point to the "\n..." within the string pointed to by pch. Then you can set that location to 0 so that pch is no longer including the "\n..."

Below is strcat example taken from

But since you are just concatentating a string to an empty string, you may as well use strcpy.

Also, in general, test for return values when calling library functions. If strstr does not find string, see Return Value to see how to detect that in:

/* strcat example */
#include <stdio.h>
#include <string.h>

int main ()
  char str[80];
  strcpy (str,"these ");
  strcat (str,"strings ");
  strcat (str,"are ");
  strcat (str,"concatenated.");
  puts (str);
  return 0;

Open in new window

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

> sprintf() removed the crash, and concatenated the string successfully
The way your are using sprintf() and strcat() are different.
If you had used sprintf() the same way as you had used strcat, that also would have crashed.
Try the following sprintf() and that will also crash the code:
sprintf(finalsid, "%s", (const char *)pch[i]);

Open in new window

The problem is, you are trying to convert "pch[ i ] which is, a character, to a string by casting it into (const char *).

So, the problem is not about strcat. It is the wrong way you are using to convert char to string.
Change your strcat() line to as shown below and that should solve the problem:
strncat((char *)finalsid, (const char*)pch + i, 1);

Open in new window

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.


    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.


JoeD77Author Commented:

fixed with:

  for(i = 0; i < 256; ++i)

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

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


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

Will ask a new question for that.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now