C++ System() Not Working When There Are Spaces in Two Different Parameters

C++ system() not working when there are spaces in two different parameters

system() runs command as cmd /C command. And here's citation from cmd doc:

If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:

1. If all of the following conditions are met, then quote characters
on the command line are preserved:

- no /S switch
- exactly two quote characters
- no special characters between the two quote characters,
where special is one of: &<>()@^|
- there are one or more whitespace characters between the
two quote characters
- the string between the two quote characters is the name
of an executable file.

2. Otherwise, old behavior is to see if the first character is
a quote character and if so, strip the leading character and
remove the last quote character on the command line, preserving
any text after the last quote character.

It seems that you are hitting case 2, and cmd thinks that the whole string C:\Users\Adam\Desktop\pdftotext" -layout "C:\Users\Adam\Desktop\week 4.pdf (i.e. without the first and the last quote) is the name of executable.

So the solution would be to wrap the whole command in extra quotes:

//system("\"D:\\test\" nospaces \"text with spaces\"");//gives same error as you're getting
system("\"\"D:\\test\" nospaces \"text with spaces\"\""); //ok, works

And this is very weird. I think it's also a good idea to add /S just to make sure it will always parse the string by the case 2:

system("cmd /S /C \"\"D:\\test\" nospaces \"text with spaces\"\""); //also works

C++ windows system ( path ) not working if there is space somewhere

If you used the command line arguments to input the string, your OS would parse it correctly. If you want to input the path while running the program, your best chance is to go with std::getline, you'll read the whole line no matter what, no need for ".

Or, if you want to implement that same parsing behavior, you'll check if the first character is " (with cin.peek()), if that's the case, you'll cin.ignore() and std::getline until another ", otherwise you'll just cin >> s;.

C# run process with multiple spaces in arguments

You probably have to add a further double-quote around anything that may include spaces within one single argument. Usually a space means the end of an argument. So to preserve this, you´d have to put the string into double-quotes.

So consolePath should actually be this:

var consolePath = "\"C:\\My Path....exe\"";

C++ system() command path with spaces

Ask yourself: how would you execute this command, yourself, from a shell prompt?

$ ls -la /home/testuser/this is a folder/test/

This, of course, will not work for the same reason your program fails. Instead, as every primer on shell scripting teaches you, you need to quote the parameter:

$ ls -la "/home/testuser/this is a folder/test/"

That will work, and you use system() in exactly the same way:

std::string command = "ls -la \"/home/testuser/this is a folder/test/\"";

But what's even better is not using system() in the first place. All the system() is, for all practical purposes, is a fork(), followed by exec() in the child process, with the parent process wait()ing for the child process's termination.

The problem is that the child process exec() the system shell, which parses the command according to its rules. This includes all the normal things that occur when executing the command via the shell directly: filename expansion, globbing, and other things.

If the string that gets passed to exec() includes any special shell characters, they'll be interpreted by the shell. In this case, you're intentionally using this to correctly parse the command string, in order to pass the correct arguments to /bin/ls.

When executing a specific, fixed command, this is fine. But when the actual command varies, or contains externally-specified parameters, it is your responsibility to correctly handle any shell wildcard characters in order to get your intended result. Hillarity ensues, otherwise. In that situation, you will find that using fork() and exec() yourself will produce far more deterministic, and reliable results, where you are in complete control of all arguments that get passed to the command being executed, instead of relying on the system shell to do it for you.

How to execute system command with an argument that contains spaces

Although depending on the operating system, my first guess is: Surround the argument by quotes like this:

int status = system("ArgumentProgram.exe User1 \"File 1\" Open");


Related Topics



Leave a reply



Submit