String to const char *

This line:

strcat(query,*it);

      

(where *it

is an iterator for a string)

Keeps this error:

there is no corresponding function to call `` strcat (char [200], const std :: basic_string, std :: allocator> &) `'

I am assuming because it strcat

takes char*

and *it

is a string. How do I convert it from string to char*

so that it works with strcat()

?

I tried strcat(query,(*it).c_str())

but it just gives me a runtime error.

Edit: sorry, it must be converted to const char*

0


source to share


9 replies


strcat(query,(*it).c_str())

must work. What's the runtime error? Are you sure you are query

null terminated before you make the call?



+7


source


Buffer overflow?



char   query[200] = {0}; // Make sure this array initialized before
                         // you start concatenating strings onto it.

for (it = vec.begin();it != vec.end();++it)
{
   if ((strlen(query) + it->length() + 1) >= 200)
   {
       logError("Buffer oveflow detected.";
       break;
   }
   strcat(query, it->c_str());
}

      

+2


source


Use a debugger, Luke!

(* it) .c_str () Sure hell should be a valid argument to strcat assuming your iterator is valid and assuming the query is a null-terminated string, so it should be The fastest way to find out which one is misbehaving is to monitor its execution and check its values ​​and execute queries at runtime.

+2


source


You need to be more specific what kind of runtime error you get. Calling c_str()

in line should be the right solution. As always with strcat()

other classic C string functions without bounds checking, you must be careful not to pass in too long input.

+1


source


If you are sure that the length of the strcat

ting in buffer is (eg 200), use strncat

; this eliminates the buffer overflow pointed out by @Martin. Otherwies checks the total length before concatenating ( this is a prerequisite for using it !)

Typically, queries are usually longer than 200 characters. If you are not sure about the duration of the resulting query, go back to a dynamic string, for example std::string

.

+1


source


Since you ruled out that it is query

not complete from a null point of view, it seems like the consensus is that the problem is likely to be one of the following:

  • buffer overflow - the buffer pointed query

    to is not large enough to (*it).c_str()

    be associated with
  • itereator,, it

    is not valid. This can happen in several ways, including:

    • was not properly initialized;
    • someContainer.end () matters;
    • or the container has been modified in some way, which invalidates the existing iterator

You should be able to determine what is happening with the debugger. Also, I'm sure if you post more code that shows how query

both are it

defined and used, you will also get a definitive answer (how to do this for remote debugging).

+1


source


Try this (I'm guessing runtime error due to NULL / invalid pointer):

for (...; it != str.end(); ++it)
...
   if (!it->empty())
   {
      strcat(query, it->c_str());
   }

      

EDIT: Sorry, c_str () never returns NULL, which I temporarily forgot, so it's always safe. If the request buffer is not long enough to of course contain all the concatenated strings (or there is some other problem such as an iterator outside of .end (), a container changed during a loop, or something similar).

0


source


Is this * pointing to a valid string in all cases? Can it point to end () on the last iteration? Or there could be the container it points to, modified, invalid * it.

0


source


If the application is in release mode, trace the application by posting messages or generating interrupt 3. (_asm int 3;) at specific locations. And if you put an interrupt, the exe will print a debug message. Attach the process to Visual Studio to debug it. Hope we can find out the location of the accident.

0


source







All Articles