0

Hi guys so I wrote a program in c++ which can me started with debugging arguments trough cmd. For example:

example.exe filepathoftxtfile

the program opens and reads the txt file. It all works fine but if the txt file lies in a path which includes spaces for example C:/Users/Jhon/test 4/space 4 the program detects it as more than one arguments because of the spacebar. Is there any way I can put in arguments in the debugging arguments and ignore the spaces for that single argument? Another thing is that I have to change the \ of the filepath to / that the program actually finds it. Is there any way I can change that?

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • 1
    OS Specific MS-Window - `example.exe "file path of txt file"` – Richard Critten Apr 28 '21 at 14:45
  • 1
    I think you don't want to ignore the spaces. You want to quote the paths. Ignoring the spaces would lead to a folder not found. Also the handling of the spaces on the command line are handled by the shell or the command line itsef. – drescherjm Apr 28 '21 at 14:46
  • ***Another thing is that I have to change the \ of the filepath to / that the program actually finds it.*** I am not sure what that means. On windows using \ is perfectly fine. You will need to escape the \ if this is a hardcoded path in your code in a string literal or use a raw string literal however you are never required to use / – drescherjm Apr 28 '21 at 15:49

1 Answers1

0

Two things:

  • About the spaces: just put the path between double quotes.
  • About the slashes: normally backslashes are used, not slashes, for indicating a path.

So you might do the following:

example.exe "C:\Users\Jhon\test 4\space 4"
Dominique
  • 16,450
  • 15
  • 56
  • 112
  • Thankss but is there any way i can type in the path without the quotes and it still works with spaces? For example can i put "filepath" in the debugging arguments? Because i want the user to just type in example.exe filepath and not example.exe "filepath" And of course yes i know that i should use \ but if i put those into the program it wont find the file. Sorry im a beginner – Cranberryjuicer21 Apr 28 '21 at 14:52
  • 1
    @Cranberryjuicer21: No, I don't think it's possible, hence my proposal of the usage of (double) quotes. By the way, how should it be possible? Just imagine you typing `example.exe A B`, how can `example.exe` know that `A` and `B` are different parameters or both belong to the same directory? This is not possible. – Dominique Apr 28 '21 at 14:55
  • If you don't want to quote can you change your paths and rename your folders so that none have spaces in them. – drescherjm Apr 28 '21 at 14:56
  • Yes thats true. Maybe its not possible then. But do you know howw i can fix the \ problem? Because i use fstream and use argv[] for it. So i used fstream myfile(argv[]). Of course i use the argument in which the user wrote the filepath and there is the problem. If it uses C:\Users\Jhon\test 4\space 4 it wont find the path but if it uses C:/Users/Jhon/test 4/space 4 it finds the path. – Cranberryjuicer21 Apr 28 '21 at 15:01
  • On a mac, you can escape the spaces, but quotes are more universal and more readable. – sweenish Apr 28 '21 at 15:02
  • ? On what platform are you working? Can you go to the mentioned directory and ask for the path? (On a UNIX-like system, type `pwd`, on a Windows-like system, just type the path, as you see it in the explorer) – Dominique Apr 28 '21 at 15:13
  • ***But do you know howw i can fix the \ problem?*** What \ problem? On windows you can use / instead of \ unless you are using a unicode extended path: [https://stackoverflow.com/questions/21194530/what-does-mean-when-prepended-to-a-file-path](https://stackoverflow.com/questions/21194530/what-does-mean-when-prepended-to-a-file-path) – drescherjm Apr 28 '21 at 15:45