[英]C++ parsing a command line
我試圖在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不被識別為內部或外部命令,可操作程序或批處理文件。”,這很奇怪,因為兩個命令處理文件路徑的方式是相同的。 請讓我知道原因。 計算機在Windows XP上運行,我使用的是Microsoft Visual Studio 2010。
更換
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");
使用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您可以使用ShellExecute或ShellExecuteEx以及前面提到的msdn上的一些文檔,這里
希望這有幫助,因為我剛剛失去了與我的家庭環境的連接,並且無法獲得我目前修復的固定版本,但今晚也會這樣做。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.