2

我试过这段代码:

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 条线。为什么会这样?

4

1 回答 1

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
于 2013-07-12T16:30:58.133 回答