如何使用路径目录的变量来保存文件?

时间:2018-02-18 15:26:07

标签: c++

我必须在文件夹中保存.txt文件。我给了目录,但它涉及多个1变量。这是代码:

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

fileNumberfileName分别是int和string类型的变量。 请告诉我正确的语法。

1 个答案:

答案 0 :(得分:1)

只需将“fileNumber”int变量转换为带有std :: to_string()的std :: string。即使你在Windows上也可以使用一个正斜杠而不是加倍反斜杠。

#include <string>
#include <fstream>

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