gpt4 book ai didi

stdout - 运行子进程并一次捕获一行输出

转载 作者:行者123 更新时间:2023-12-04 04:49:43 24 4
gpt4 key购买 nike

我试过这个代码:

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 行。为什么会发生这种情况?

最佳答案

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

关于stdout - 运行子进程并一次捕获一行输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17619661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com