Solved

strcat causing crash when concatenating char

Posted on 2011-02-23
6
683 Views
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')
	{
		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!
0
Comment
Question by:JoeD77
6 Comments
 
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
0
 
LVL 84

Expert Comment

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

Accepted Solution

by:
phoffric earned 250 total points
ID: 34967901
The following discussion assumes that you understand pointers and arrays. For review, here is a tutorial:
     http://www.cprogramming.com/tutorial/lesson6.html

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
      http://www.cplusplus.com/reference/clibrary/cstring/strcat/

But since you are just concatentating a string to an empty string, you may as well use strcpy.
      http://www.cplusplus.com/reference/clibrary/cstring/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:
    http://www.cplusplus.com/reference/clibrary/cstring/strstr/

/* 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

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 8

Assisted Solution

by:ssnkumar
ssnkumar earned 250 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

0
 
LVL 33

Expert Comment

by:sarabande
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.

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



0
 

Author Closing Comment

by:JoeD77
ID: 34972257
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.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

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…
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…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

829 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