'C++ Move file Char Issue

  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 );

This is my code, I'm basically trying to move files around directories in c++. But whenever I run my code it throws this error:

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

Does anyone know how to fix this?

c++


Solution 1:[1]

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

You can't concatenate string literals like that. You could use std::strcat for that but it's easier and less error prone to use the string and filesystem libraries:

#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);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1