我试过这段代码:
Local $foo = Run(@ComSpec & " /c dir", '', 0, 2)
Local $line
While 1
$line = StdoutRead($foo)
If @error Then ExitLoop
$line = StringStripCR($line)
If StringLen($line) > 0 Then ConsoleWrite("START" & $line & "END" & @CRLF)
WEnd
我希望一次获得一行,但我得到2,3或50行。为什么会这样?
答案 0 :(得分:3)
StdoutRead()
不会按换行符分割,只会返回数据块。以下代码将数据解析为行:
Local $foo = Run(@ComSpec & " /c dir", '', 0, 2)
Local $line
Local $done = False
Local $buffer = ''
Local $lineEnd = 0
While True
If Not $done Then $buffer &= StdoutRead($foo)
$done = $done Or @error
If $done And StringLen($buffer) == 0 Then ExitLoop
$lineEnd = StringInStr($buffer, @LF)
; last line may be not LF terminated:
If $done And $lineEnd == 0 Then $lineEnd = StringLen($buffer)
If $lineEnd > 0 Then
; grab the line from the front of the buffer:
$line = StringLeft($buffer, $lineEnd)
$buffer = StringMid($buffer, $lineEnd + 1)
ConsoleWrite("START" & $line & "END" & @CRLF)
EndIf
WEnd