integer in strcat()

Jun 13, 2012 at 11:40am
I have a
1
2
3
4
int a=10;  //or let's say  int=9;
//and combine it to a ".txt" using strcat

strcat(a,".txt");


I already tried using
1
2
3
4
5
6
int a=10;
string h;

h=a;

strcat(h.c_str(),".txt");

But there's a error after compiling..
cannot convert parameter 1 from 'const char *' to 'char *'

I still need to retain integer 10, 9, 100 to '10', '9', '100' and not converting it to symbols and letters, etc..

what else can I do?
Last edited on Jun 13, 2012 at 11:45am
Jun 13, 2012 at 11:48am
Last edited on Jun 13, 2012 at 11:49am
Jun 13, 2012 at 12:10pm
@coder
it just converts to the characters based at the ascii table..
10 is a new line so when I run it.. it shows

.txt


I need 10 -> '10' not 10 -> new line
Jun 13, 2012 at 12:16pm
Use _itoa to store your charized 10 into a temporary buffer, then add it to your string. Don't ever use stdio functions with C++ strings. Especially strcat. Buffer Overflow/Seg Fault may happen.
Jun 13, 2012 at 12:17pm
Okay, so I misinterpreted what you said.

Then use stringstream:

http://www.cplusplus.com/reference/iostream/stringstream/

You can do all kinds of formatting:
1
2
3
4
5
6
7
#include <sstream>

stringstream ss;

ss << 10 << ".txt";

string h = ss.str();
Topic archived. No new replies allowed.