You can't cast an integer into a string, that's just not possible in C.
You need to use an explicit formatting function to construct the string from the integer. My favorite is snprintf()
.
Once you realize that, you can just as well format the entire filename in a single call, and do away with the need to use strcat()
(which is rather bad, performance-wise) at all:
snprintf(filename, sizeof filename, "./speeches/speech%d", a);
will create a string in filename
constructed from appending the decimal representation of the integer a
to the string. Just as with printf()
, the %d
in the formatting string tells snprintf()
where the number is to be inserted. You can use e.g. %03d
to get zero-padded three-digits formatting, and so on. It's very powerful.