Open Pipe
 
为文件操作打开外部进程的标准输入(stdin )或输出(stdout )流。

语法

Open Pipe shell_command For Input As [#]filenumber
Open Pipe shell_command For Output As [#]filenumber
Open Pipe shell_command For Binary access_type [#]filenumber

用法

result = Open Pipe( command , For {Input|Output}, As filenumber )
or,
result = Open Pipe( command , For Binary, access_type , As filenumber )
(or in the QB-like syntax,)
Open Pipe filename For {Input|Output} As filenumber
(or,)
Open Pipe filename For Binary access_type As filenumber

参数

shell_command
在操作系统命令shell中执行的外部进程。相对文件路径是相对于当前目录(参见CurDir)。当为其可执行路径或其参数中需要双引号的进程打开管道时,整个管道字符串应嵌套在双引号内。
access_type
调用进程请求的读或写访问类型。
      • Access{Read| Write}(either the stdin or stdout stream of the external process can be opened)
filenumber
一个可用的文件编号绑定到外部进程'stdin stdout 流。

返回值

在第一次使用中,Open Pipe成功返回零(0),否则返回非零错误代码。

说明

Open Pipe在命令shell中执行另一个进程,并打开其stdin stdout 流以进行读取或写入。A file number 绑定到流,用于后续文件操作,例如Input #.可以使用FreeFile检索可用的filenumber .如果外部进程不存在,则抛出运行时错误。

InputOutputfile modes 分别打开用于读取或写入纯文本的顺序文本I / O的外部进程“stdin stdout 流。然后可以使用文本模式文件操作(例如Line Input #Print #)读取或写入字符,单词或整行。

Binaryfile mode 将打开外部流程“stdin ”或stdout 个流,具体取决于指定的access type (参见上述access_type 参数的说明) - 随机访问读取或写入任意大小并解释原始数据。简单的数据类型值(如ByteLongInt)和整个存储器块可以使用二进制文件操作(如Get #Put #)读取或写入流中。
FB不支持双向管道,必须使用OS的API功能实现。

运行时错误:
Open Pipe会抛出以下运行时错误之一:

(1) Illegal function call
    • filenumber 当时不是免费的。使用FreeFile确保filenumber 免费。

例子

''此示例使用Open Pipe运行shell命令并检索其输出。
#ifdef __FB_UNIX__
Const TEST_COMMAND = "ls *"
#else
Const TEST_COMMAND = "DIR *。*"
#endif

Open Pipe TEST_COMMAND For Input As #1

Dim As String ln
Do Until EOF(1)
    Line Input #1, ln
    Print ln
Loop

Close #1


平台差异

  • 所有平台不支持二进制file mode 打开管道如果无法以二进制模式打开外部进程“stdin ”或stdout 个流,则将抛出错误。

与QB差别

  • 新的FreeBASIC

参考