-2

How can I get the path from CMD cointaning gaps " "? Here is the code I tried without success:

if (argv[3] == NULL) {
    cout << "" << endl;
}
else if (strcmp(argv[3], "/d") == 0) {
    const size_t cSize = strlen(argv[4]) + 1;
    wchar_t* wc = new wchar_t[cSize];
    mbstowcs(wc, argv[4], cSize);
    adresar = wc;
    cout << "/d OK" << endl;
}
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
Mike Litoris
  • 27
  • 1
  • 6
  • 2
    Unrelated to your problem, but I assume you check `argc` to make sure there *are* at least three or four arguments? Because if there's none (for example) then `argv[3]` is out of bounds and you will have *undefined behavior*. – Some programmer dude Aug 07 '17 at 09:39
  • 2
    More related to your problem, if you want to pass an argument containing *spaces* then enclose it in double-quotes. Like `mycommand argv1 argv2 /d "argv4 with spaces"` – Some programmer dude Aug 07 '17 at 09:41
  • It is a path to some directory and when I enclose it in double quotes "" it is not working, I read something about UTF coding but I dont know how to remake my code. Example: "C:\Users\xxx\Documents\Visual Studio 2017\Projects\ConsoleApplication1\ConsoleApplication1" – Mike Litoris Aug 07 '17 at 10:12
  • The path is transmitted by argv[4] – Mike Litoris Aug 07 '17 at 10:16
  • I'd recommend putting the command-line array in a `std::vector` first, then you can code a lot easier. Takes just two lines... `for (... i < argv ...) push_back(args[i])`. Plus you _can already find_ tons of questions related to string parsing. – Blacktempel Aug 07 '17 at 10:19
  • How can I convert CONST wchar_t to LPWSTR? – Mike Litoris Aug 07 '17 at 10:48

1 Answers1

0
mycommand argv1 argv2 /d "argv4 with spaces
Mike Litoris
  • 27
  • 1
  • 6
  • 2
    Although this code might solve the problem, a good answer should always contain an explanation. – BDL Aug 08 '17 at 12:00