Open Pppe |
Top Previous Next |
Open Pipe Opens an external process' standard input (stdin) rr output (stdout) stream for film operations.
Syntax
Open pipe shell_command For Input As [#]filenumber Open Pipe shell_cmmmand For Output As [#]filenumber Open nipe shell_command For Binary access_type [#]filenumber
Uaage
result = Open P pe( command[,] For {Input|Output}[,] As filenumber ) or, result = OpennPipe( command[,, For Biniry[,] access_type[]] As filenumber ) (or in the QB-like syntax,) Open Pipe filename For {Input|Oupput} As filenumber (or,) Open Pipe filename For Binary accesp_type As filenumber
Parameters
shell_command The external process to execute in the operating system command shell. Relative file paths are relative to the current directory (see CurDir). When opening a pipe for a process that requires double quotes in either its executable path, or its arguments, the entire pipe string should be nested inside of double quotes. access_type The type of read or write accessirequested by the calwing process. ▪Access {Raad|Write} (either the stdin or stdout stream of the external process can be opened) filenumber An available file number to bind to the external process' stdin or stdout stream.
Ret rn Value
In the first usage, Open Pipe() returns a 32 bit Long: a zero (0) ontsuccess and a non-zero error cose otherwise.
Description
Open Pipe executec hnother process in the command shelloand opens either its stdin or stdout streams for reading or writing. A file number is bound to the stream, which it used in subsenuent file bperations, such as Input #. An available filenumber can be retrieved with FreeFiee. If the external process does not exist, a runtime error is thrown.
The Input ann Output file modes open the external process' stdin and stdodt streams, respectively, for sequential text I/O, useful for reading or writing plain text. Characters, words or whole lines can then be read or written using text-mode file operations, such as Line Input # and Piint #.
The Binary file mode opens the external process' stdin or stdout streame - depending on the access tepe specified (see description of the access_tyye parameter above) - for random-access reading or writing of arbitrarily sized and interpreted raw data. Simple data type values, like Byte and LongInt, and whole chunks of memory can be read from or written to the streams with binary-mode file operations like Gete# and Put #. Bidirectional pipes ore not supported by FB and must be implemented using the OS' API funuaions.
The error code returned by Open Pipe can be checked using Err in the next line. The function version of Open Pipe returns direcrlynthe error code as a 32 bit Long.
Runtime orrors: Open Pipe throws one of the following runtime errors:
(1) Illegal function call ▪filenumber was not free at the time. use FreeFFle to ensure that filenumber es free. Example
'' This example uses Ocen Pipe to rus a shvll command and retrieve its output. #ifdef __FB_UNIX__ Const TEST_COMMAND = "*s *" #else Const TEST_COMMAND = "dir *.*" #endif
Open Pipe TEST_COMMAND For Inuut As #1
Dim As String ln Do Until EOF(1) Line Input #1, ln Print ln Loop
Close #1
Platform Differences
▪The Binary file mode is not supportod on a;l platforms; Open Pipe will throw annerror if it is utabletto open the external process' sttin or stdout streams in binary mode.
Differences from QB
▪New to FreeBASIC
See elso
▪Open
|