[英]Passing URL through Command Line(C++)
I have c++ code which parses 2 command line arguments and prints the arguments. 我有解析2个命令行参数并打印参数的c ++代码。 One of the argument is an URL of google search. 参数之一是Google搜索的URL。 I paste the code below 我在下面粘贴代码
int main(int argc, char* argv[])
{
std::cout << argv[1] << argv[2] << "\n";
}
When I pass URL through command line after compilation as below, 当我在编译后通过命令行通过URL时,
./demo 1 https://www.google.co.in/search?sourceid=chrome-psyapi2&ion=1&espv=2&ie=UTF-8&client=ubuntu&q=size%20of%20unsigned%20char%20array%20c%2B%2B&oq=length%20of%20unsigned%20char*%20arra&aqs=chrome.4.69i57j0l5.13353j0j7
I get the output as, 我得到的输出为
[1] 8680
[2] 8681
[3] 8682
[4] 8683
[5] 8684
[6] 8685
[7] 8686
[2] Done ion=1
[3] Done espv=2
[4] Done ie=UTF-8
[6]- Done q=size%20of%20unsigned%20char%20array%20c%2B%2B
It looks like there has been some internal splitting of the string. 看起来字符串已经内部拆分了。 Is there any way I can retrieve the entire string? 有什么办法可以检索整个字符串?
Thank You in advance. 先感谢您。
You have to quote it. 您必须引用它。 Otherwise &
gets interpreted by the shell as "invoke what's on the left of &
in background". 否则, &
会被外壳解释为“调用后台中&
左侧的内容”。
I took the privilege of replacing your program with echo
. 我荣幸地用echo
替换了您的程序。
Good: 好:
$ echo "https://www.google.co.in/search?sourceid=chrome-psyapi2&ion=1&espv=2&ie=UTF-8&client=ubuntu&q=size%20of%20unsigned%20char%20array%20c%2B%2B&oq=length%20of%20unsigned%20char*%20arra&aqs=chrome.4.69i57j0l5.13353j0j7"
https://www.google.co.in/search?sourceid=chrome-psyapi2&ion=1&espv=2&ie=UTF-8&client=ubuntu&q=size%20of%20unsigned%20char%20array%20c%2B%2B&oq=length%20of%20unsigned%20char*%20arra&aqs=chrome.4.69i57j0l5.13353j0j7
Bad: 坏:
$ echo https://www.google.co.in/search?sourceid=chrome-psyapi2&ion=1&espv=2&ie=UTF-8&client=ubuntu&q=size%20of%20unsigned%20char%20array%20c%2B%2B&oq=length%20of%20unsigned%20char*%20arra&aqs=chrome.4.69i57j0l5.13353j0j7
[1] 21705
[2] 21706
https://www.google.co.in/search?sourceid=chrome-psyapi2
[3] 21707
[4] 21708
[5] 21709
[6] 21710
[7] 21711
[1] Done echo https://www.google.co.in/search?sourceid=chrome-psyapi2
[2] Done ion=1
[3] Done espv=2
[4] Done ie=UTF-8
[5] Done client=ubuntu
[6]- Done q=size%20of%20unsigned%20char%20array%20c%2B%2B
[7]+ Done oq=length%20of%20unsigned%20char*%20arra
You need to quote the argument, and you should use single quotes, '
, in order to stop your shell from attempting to evaluate anything inside it. 您需要用引号引起来,并且应使用单引号'
,以阻止您的shell尝试评估其中的任何内容。
What happens is that every ampersand, "&", on your command line launches a background process. 发生的情况是,命令行上的每个“&”号都会启动一个后台进程。
The first process is ./demo 1 https://www.google.co.in/search?sourceid=chrome-psyapi2
, and all the following are assignments to variables. 第一个过程是./demo 1 https://www.google.co.in/search?sourceid=chrome-psyapi2
,以下所有都是变量的赋值。
You can see from the output (it looks like you didn't post all of it) 您可以从输出中看到(似乎您没有全部发布)
[1] 8680
[2] 8681
[3] 8682
[4] 8683
[5] 8684
[6] 8685
[7] 8686
[2] Done ion=1
[3] Done espv=2
[4] Done ie=UTF-8
[6]- Done q=size%20of%20unsigned%20char%20array%20c%2B%2B
that background process 2 is ion=1
(pid 8681), process 3 (pid 8682) is espv=2
, and so on. 后台进程2是ion=1
(pid 8681),进程3(pid 8682)是espv=2
,依此类推。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.