Solved

strcat causing crash when concatenating char

Posted on 2011-02-23
6
694 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
[X]
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
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
Independent Software Vendors: 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!

 
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 34

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create a path if not exists 7 124
windows 10 pro lost profile. 10 85
Adjust the codes 3 63
Excel formula to calculate ID # 4 41
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

732 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