-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
  • 208,748
  • 37
  • 389
  • 560
De Kirvis
  • 117
  • 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,614
  • 3
  • 28
  • 50
0
GetTempPathA(MAX_PATH, tempPath);
string fullPath = (string)tempPath + "/" + data.fileName;

std::replace(fullPath.begin(), fullPath.end(), '\\', '/');
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
0

You may just use this:

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

and everything will be fine

Live Demo

Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160