[Webinar] Streamline your web hosting managementRegister Today


strcat causing crash when concatenating char

Posted on 2011-02-23
Medium Priority
Last Modified: 2012-05-11
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

Question by:JoeD77
LVL 17

Expert Comment

by:Shinesh Premrajan
ID: 34967674
Instead of the 256 in the for loop, it should be  the size of the pch.

Hope this helps
LVL 85

Expert Comment

ID: 34967686
buffer and pch may not be null terminated
LVL 33

Accepted Solution

phoffric earned 1000 total points
ID: 34967901
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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.


Assisted Solution

ssnkumar earned 1000 total points
ID: 34968402
> 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

LVL 35

Expert Comment

ID: 34969008
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.



Author Closing Comment

ID: 34972257

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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

640 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