C++解析命令行

3

我想在C++程序中运行两个命令行,但出现了奇怪的错误。我想要运行的命令行是

vlc -vvv dshow:// :dshow-vdev='USB Video Device' :dshow-adev=""  :live-caching=50 :sout=#transcode{vcodec=WMV2,vb=800,acodec=wma2,ab=128,channels=2,samplerate=44100}:duplicate{dst=udp{dst=localhost:1234},dst=display} :sout-keep

vlc -vvv udp://@localhost:1234:network-caching=50

这两个程序在命令提示符中都可以正常运行。但是当通过C++系统shell调用运行它们时,第一个程序会失败,而第二个会成功。我在C++中运行它们的方式如下:

system( "\"G:/Program Files/VideoLAN/VLC/vlc\" -vvv dshow:// :dshow-vdev=\"USB Video Device\" :dshow-adev=\"none\"  :live-caching=50 :sout=#transcode{vcodec=WMV2,vb=800,acodec=wma2,ab=128,channels=2,samplerate=44100}:duplicate{dst=udp{dst=localhost:1234},dst=display}:sout-keep");

system( "\"G:/Program Files/VideoLAN/VLC/vlc\" -vvv udp://@localhost:1234:network-caching=50");

第一个命令会抛出一个错误:“G:/Program is not recognized as an internal or external command, operable program or batch file.”,这很奇怪,因为这两个命令处理文件路径的方式是相同的。请告诉我原因。计算机运行在Windows XP系统上,我正在使用Microsoft Visual Studio 2010。

在您的路径中,在空格之前添加\。 - Zeta
但是第二个命令中空格前面没有 \,但也可以运行。无论如何感谢您的帮助!我会尝试这样做! - Vigo
添加 \ 也不起作用! - Vigo
尝试将整个系统cmd放入char*变量中。先尝试打印字符串,然后再将该变量传递给system。这样您就可以确定cmd的解析是否正确。 - Abhineet
让我们在聊天中继续讨论 - Vigo
显示剩余4条评论
2个回答

0

替换

system( "\"G:/Program Files/VideoLAN/VLC/vlc\" -vvv dshow:// :dshow-vdev=\"USB Video Device\" :dshow-adev=\"none\"  :live-caching=50 :sout=#transcode{vcodec=WMV2,vb=800,acodec=wma2,ab=128,channels=2,samplerate=44100}:duplicate{dst=udp{dst=localhost:1234},dst=display}:sout-keep");

system( "\"G:/Program Files/VideoLAN/VLC/vlc\" -vvv udp://@localhost:1234:network-caching=50");

使用

system( "G:\\\"Program Files\"\\VideoLAN\\VLC\\vlc -vvv dshow:// :dshow-vdev=\"USB Video Device\" :dshow-adev=\"none\"  :live-caching=50 :sout=#transcode{vcodec=WMV2,vb=800,acodec=wma2,ab=128,channels=2,samplerate=44100}:duplicate{dst=udp{dst=localhost:1234},dst=display}:sout-keep");

system( "G:\\\"Program Files\"\\VideoLAN\\VLC\\vlc -vvv udp://@localhost:1234:network-caching=50");

也不起作用,因为文件路径被读取为"G:/"Program Files"/VideoLAN/VLC/vlc"。我认为额外的引号导致了问题。会出现错误"The filename, directory name or volume label syntax is incorrect." - Vigo
请告诉我编辑后的解决方案是否适用于您。 - Abhineet
不是的,它并没有!又出现了相同的错误:“文件名、目录名或卷标语法不正确。” - Vigo
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/40712/discussion-between-vigo-and-abhineet - Vigo
我错过了,请问您的最终版本是哪个?谢谢。 - Vigo
显示剩余3条评论

0

使用system()运行命令有以下注意事项和开关要求,针对您正在尝试的操作(来自cmd文档),因为它实际上运行了cmd /C *mycommandhere*

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.

如果您将命令重新格式化为:

    system("cmd /S /C \"\"c:\\testDir\"\"\\dir with spaces\"\"\\myexe.exe");

应该可以工作:)(在以前的版本中,我忘记了一些转义字符)。

这里还有一个相关问题。

或者如果只是针对Windows,您可以利用ShellExecuteShellExecuteEx,并在msdn, 这里上提供一些文档。

希望这可以帮助,因为我刚刚失去了与家庭环境的连接,目前无法获得之前修复的固定版本,但今晚会这样做。


这会出现“文件名、目录名或卷标语法不正确”的错误! - Vigo
我会重新检查我输入的格式是否正确,可能会有遗漏,抱歉。 - GMasucci

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接