I need to be able to tell if the final argument in my command line is surrounded in double quotes or not. If it's in double quotes, I treat it as a string. If it's not, I need to treat it as a file to open and obtain the string. Argv by default will grab the double quoted string and strip the quotes, so I can't figure out a way to handle this problem.
pseudocode is something like this...
if(argv[argc-1] was called with surrounding double quotes){
//handle as string (I already have code to do this)
}
else{
//handle as filename (I already have code to do this)
}
All of the parameters in argv are strings. You are probably better off rethinking your strategy. Try opening the argument, if that fails then treat it as a string.
Alternatively you could escape the quotes on the command line and they will be passed to your application:
$ program "\"this is a string\""
Edit: The sample code assumes you are using a Bash shell or something similar
Related
I have a simple C++ program that lists the arguments it gets:
#include <iostream>
int main(int argc, char* argv[]) {
for (int i = 1; i < argc; i++) {
std::cout << argv[i] << std::endl;
}
return 0;
}
I'm just testing/figuring this out but the ultimate intention is for this to accept the names of some files and directories.
So I pass it these arguments:
"\\server\directory\file.ext" "C:\trailing\backslash\" "file.txt"
But this is what's printed:
\\server\directory\file.ext
C:\trailing\backslash" file.txt
i.e. the trailing slash on the second argument makes it think the closing quote is escaped.
I'm setting the arguments in the property pages of VS2017, but I get the same output when:
I call the exe from PowerShell.
I allow an external tool to pass my
exe arguments that it has built (which will be how this is ultimately
used).
How can I get my program to understand that the quoted path with a trailing slash is one argument?
EDIT
Section 4 of this article describes exactly my problem. Basically I want to make my C++ program interpret arguments in the same way that a batch file or VBScript does. I cannot change how the arguments come in to my program
EDIT I will simplify the question:
The C++ program above behaves like this:
I want it to behave like this:
What do I have to do to the program or compiler in order for that to happen?
You have to protect the trailing backslash with another backslash, otherwise the backslash is treated as an escape character for the following double quote.
See this blog post by Raymond Chen for a rationale for this behavior.
Here is a quote of the relevant part:
A string of backslashes not followed by a quotation mark has no special meaning.
An even number of backslashes followed by a quotation mark is treated as pairs of protected backslashes, followed by a word terminator.
An odd number of backslashes followed by a quotation mark is treated as pairs of protected backslashes, followed by a protected quotation mark.
I tried compiling this with an extra slash to the second argument like below and was able to produce the desired output !
"\\server\directory\file.ext" "C:\trailing\backslash\\" "file.txt"
This was the output
/home/a.out
\server\directory\file.ext
C:\trailing\backslash\
file.txt
Upvote my post if it helps you :D
If you are not happy with the command line parsing you can do it by your own:
Just use GetCommandLine and parse your command line as you want it.
Other post already described how the command line is/should be treated (see blog post of Raymond Chen).
I am trying to run a C++ application where I am passing some command line arguments to it as follows:
./startServer -ip 10.78.242.4 tcpip{ldap=no;port=2435}
The application is getting crashed because it is not able to get the correct port. Searching over the web, I found that ";" is treated an end of command character (Semicolon on command line in linux) so everything after that is getting ignored. I also understand the putting it inside the quotes will work fine. However, I do not want to force this restriction of putting the arguments in the quotes on the users. So, I want to know is there a way I can process the ";" character with the argv array?
The semicolon separates two commands so your command line is equivalent to
./startServer -ip 10.78.242.4 tcpip{ldap=no
port=2435}
Your application will never know anything about either the semi colon or the second command, these will be completely handled by the shell. You need to escape the colon with a back slash or enclose it in quotes. Other characters which may cause similar issues include: $,\-#`'":*?()&|
Complex strings are much easier to pass either from a file or through stdin.
You need to quote not only the ; but in the general case also the { and }:
./startServer -ip 10.78.242.4 'tcpip{ldap=no;port=2435}'
If your users are required to type in that complicated last argument, then they can also be made to quote it.
from subprocess import *
s=Popen(['C:\Python27\Scripts\pyssim',"'C:\Users\P\Desktop\1.png'",'C:\Users\P\Desktop\2.png'],stderr=PIPE,stdout=PIPE,shell=True)
out,err=s.communicate()
print out
The python program above executes successfully but it shows no output.
Nothing is printed on the shell.
While running command on cmd it gives output "1".
Your command is failing because the parameters being passed to it are not what you think they are; keep in mind that backslashes are normally treated as the start of escape sequences in Python string literals. Specifically, the \1 and \2 are being treated as octal character escapes, rather than digits. If you looked at the contents of err, you would probably find something like a file not found error. Some possible solutions:
Double all of the backslashes, to escape them.
Put an 'r' in front of each string literal, to make them 'raw strings' that don't specially interpret backslashes.
Not actually applicable in this case, but you can often just use forward slashes instead - most of Windows will happily accept them instead of backslashes, the one exception being the command line (which is what you're actually invoking here).
I am trying to execute a file with parameters using the "system()" function in C++ on Windows, and it works as long as there are no whitespaces in the filename. For parameters, putting double quotes around the string works, but when I try the same with the executable itself, I get the following error:
"the filename,directory name, or volume label syntax is incorrect"
Does anyone know how to handle this correctly?
Use a string like this:
cmd /S /C "your entire command line string"
See: How do I deal with quote characters when using cmd.exe
It should work, look for the problem elsewhere.
Perhaps something in your flow is removing the whitespace or the double quotes from the string.
First off, I'm a complete beginner at C++.
I'm coding something using an API, and would like to pass text containing new lines to it, and have it print out the new lines at the other end.
If I hardcode whatever I want it to print out, like so
printInApp("Hello\nWorld");
it does come out as separate lines in the other end, but if I retrieve the text from the app using a method that returns a const char then pass it straight to printInApp (which takes const char as argument), it comes out as a single line.
Why's this and how would I go about to fix it?
It is the compiler that process escape codes in string literals, not the runtime methods. This is why you can for example have "char c = '\n';" since the compiler just compiles it as "char c = 10".
If you want to process escape codes in strings such as '\' and 'n' as separate characters (eg read as such from a file), you will need to write (or use an existing one) a string function which finds the escape codes and converts them to other values, eg converting a '\' followed by a 'n' into a newline (ascii value 10).