-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
  • 730,956
  • 141
  • 904
  • 1,278
Sikander
  • 2,799
  • 12
  • 48
  • 100

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
  • 114,268
  • 16
  • 167
  • 218