Discussion Locked

C++ type casts

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
Is there a way to type cast an int var to a char[] variable. i need to take an integer variable and use it in part of a file name infile.open(filename) but i need to concatenate it with other char[].

3 replies

by nikki96

I don't know about C++, but it C it's easy.

/* my Int will be set somewhere */
int myInt;

/* set length depending on size of int */
char str[38];
char myFilename[100];

/* further in the program, you want myInt as str */
sprintf(str, "%d", myInt);
/* or say you want it all together as a filename*/
sprintf(str, "%s%d", myFilename, myInt);

If you include the correct library (stdio.h), you can use these functions in C++.

by whitey_brw

Thanks alot!! i have been trying to use typecasting and reinterpert type casting but nothing worked. Your code worked perfectly. thanks again.

This discussion is closed to new replies.