Converting int to char

Hello

I need to be able to convert an integer to a char.
I've already tried several things but none worked.

1
2
3
int a = 5;
char file[] = "file" + a + ".txt";
remove(file);


This is the error G++ gives:
test.cpp:6:28: error: invalid operands of types ‘const char*’ and ‘const char [5]’ to binary ‘operator+’


Thanks for reading,
Niely
Last edited on
You want "sprintf()" to modify a string like that: http://www.cplusplus.com/reference/cstdio/sprintf/

Make sure that your receiving buffer is large enough.

EDIT: <REMOVED> I thought about it for a minute and my other suggestion is more aggravation then it is worth. std::stringstreams are another option, but I personally don't like them.
Last edited on
^How would that go with stringstream?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include <iostream>

int main()
{
    int a = 5;
    std::string filename = "file" + std::to_string(a) + ".txt";

    std::cout << "The filename is: " << filename << std::endl;

    // if you need a const char*:
    const char* ptr = filename.c_str();
    std::cout << "As a pointer:  " << ptr << std::endl;
}



http://ideone.com/SDc4gt
Thanks all!
That worked out well.
Topic archived. No new replies allowed.