-2

I have a string path to my file that I want to execute. It is for example:

E:\folderA\folderB\myfile.exe

If I write this path and I try to execute file there it says that file doesn't exist.

When I write it like that. Then it works.

  E:/folderA/folderB/myFile.exe

How do I change \ to / ?

Paul R
  • 202,568
  • 34
  • 375
  • 539
De Kirvis
  • 107
  • 1
  • 9

3 Answers3

2

Windows is quirky about whether Unix (/) or windows (\) separators are accepted.

You also need to escape '\' in a string

const char * bad = "C:\hello\world.txt"
const char *good = "C:\\hello\\world.txt"

std::string::replace allows substitution.

mksteve
  • 12,218
  • 3
  • 25
  • 48
0
GetTempPathA(MAX_PATH, tempPath);
string fullPath = (string)tempPath + "/" + data.fileName;

std::replace(fullPath.begin(), fullPath.end(), '\\', '/');
fredoverflow
  • 246,999
  • 92
  • 370
  • 646
0

You may just use this:

std::string str=R"(E:\folderA\folderB\myfile.exe)";

and everything will be fine

Live Demo

Humam Helfawi
  • 18,595
  • 13
  • 73
  • 147