-1
  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?

Aakash Shah
  • 49
  • 1
  • 1
  • 2
  • You can't just add pointers. What you want is string concatenation. Instead of `char` arrays, use `std::string`. Don't make your life harder than it needs to be. – JohnFilleau Mar 19 '22 at 21:45
  • Does this answer your question? [Assign a string literal to a char\*](https://stackoverflow.com/questions/8356223/assign-a-string-literal-to-a-char) – ChrisMM Mar 19 '22 at 21:48
  • @ChrisMM The variables are arrays here, not `char*`. – user17732522 Mar 19 '22 at 21:52
  • Does this answer your question? [invalid operands of types 'const char\*' and 'const char \[93\]' to binary](https://stackoverflow.com/questions/30627073/invalid-operands-of-types-const-char-and-const-char-93-to-binary) or https://stackoverflow.com/questions/23936246/error-invalid-operands-of-types-const-char-35-and-const-char-2-to-binar – user17732522 Mar 19 '22 at 21:54
  • @user17732522, you're right. Grabbed the wrong dup – ChrisMM Mar 19 '22 at 22:40
  • Aakash, did the answer help? Please ask if you need clarification. – Ted Lyngmo Mar 20 '22 at 21:29

1 Answers1

3

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);
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108