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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 84

Expert Comment

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

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

Technology Partners: 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!


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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

743 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