-1

I have to concatenate integer with a string as follows, user will enter a number e.g. 1 and it will be be placed in string like this:

std::remove("C:/Users/pcname/Desktop/files/1.txt");

If user enters 2, it's like

std::remove("C:/Users/pcname/Desktop/files/2.txt");

It's pretty basic but I'm having issue with this I tried to use operator+ with this but that did not work.

Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229
Sikander
  • 2,718
  • 10
  • 44
  • 90

1 Answers1

1

You can use std::to_string to convert an integer to a std::string, then use concatenation

int file_num = 1;
std::remove("C:/Users/pcname/Desktop/files/" + std::to_string(file_num) + ".txt");

Otherwise trying to do something like

"C:/Users/pcname/Desktop/files/" + file_num

is actually doing pointer arithmetic and will not produce the string you think it will

Cory Kramer
  • 107,498
  • 14
  • 145
  • 201