C++移动文件字符问题


int result;
char old[]="/home/aakash/Downloads/10_Test_Iterations/10 Test Iterations/test";
char oldname[] ="/home/aakash/memphys/memphys-main/examples/navier_stokes_2D/test"+((char)n)+"_polydeg_3_fields_gmsh.csv";
char newname[] =old+((char)n)+"/_polydeg_3_fields_gmsh.csv";
result= rename( oldname , newname );

这是我的代码,我基本上是在尝试在c++中的目录中移动文件。但每当我运行代码时,它都会抛出以下错误:

error: invalid operands of types ‘char*’ and ‘const char [5]’ to binary ‘operator+’

有人知道怎么解决这个问题吗?

char oldname[] ="/home/aakash/memphys/memphys-main/examples/navier_stokes_2D/test"+((char)n)+"_polydeg_3_fields_gmsh.csv";

不能像那样连接字符串文字。您可以使用std::strcat,但使用stringfilesystem库更容易出错:

#include <filesystem>
#include <string>
// ...
std::string old =     "/home/aakash/Downloads/10_Test_Iterations/10 Test Iterations/test";
std::string oldname = "/home/aakash/memphys/memphys-main/examples/navier_stokes_2D/test" +
std::to_string(n) + "_polydeg_3_fields_gmsh.csv";
std::string newname = old + std::to_string(n) +
"/_polydeg_3_fields_gmsh.csv";
std::filesystem::rename(oldname, newname);

最新更新