-4

I have to save a .txt file in a folder. I have given the directory but it involves more then 1 variable. Here is the code:

ofstream outfile;
outfile.open(("c:\\User\\Taha Mukhtar\\source\\repos\\ConsoleApplication3\\ConsoleApplication3\\Folder"+fileNumber+"\\"+fileName);

The fileNumber and fileName are variables of the type int and string respectively. Please tell me the correct syntax.

Ron
  • 14,674
  • 4
  • 34
  • 47

1 Answers1

1

Just convert the "fileNumber" int variable to a std::string with std::to_string(). Also even if you're on Windows you can just use a single forward slash instead of doubling the backslashes.

#include <string>
#include <fstream>

std::ofstream outfile;
outfile.open("c:/User/Taha Mukhtar/source/repos/ConsoleApplication3/ConsoleApplication3/Folder" + std::to_string(fileNumber) + "/" + fileName);
Zebrafish
  • 11,682
  • 3
  • 43
  • 119