Open Pipe example does not work

Forum for discussion about the documentation project.
Post Reply
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Open Pipe example does not work

Post by Richard »

The Open Pipe example in the FreeBASIC Manual help does not work because "fbc.exe" is not recognised as an internal or external command, or program when run under FBIde.
Maybe this example should create its own command or use a different command that is always available.

Code: Select all

Dim text As String
Open Pipe "fbc.exe" For Input As #1
Print "Output of fbc:"
Do While Not EOF(1)
   Line Input #1, text
   Print text
Loop
sleep
axipher
Posts: 891
Joined: Dec 27, 2005 16:37
Location: Sudbury,Ontario

Post by axipher »

Very simple fix, the examples assume that you compile all sources in the directory you installed FreeBASIC. Simply change where it says "fbc.exe" to the location of where you installed FreeBASIC.
DrV
Site Admin
Posts: 2116
Joined: May 27, 2005 18:39
Location: Midwestern USA
Contact:

Post by DrV »

It's also possible to add FreeBASIC's installation directory to your PATH environment variable, which makes it possible to run fbc from any directory, ex. from a command prompt.
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Post by Richard »

There is an extra double quote in the "ChDir" example, I have used it with the "Open pipe" example. This combination does work.

Code: Select all

Dim pathname As String = "C:\program files\FreeBASIC"
Dim result As Integer = ChDir(pathname)
If 0 <> result Then Print "error changing current directory to " & pathname & "." 

Dim text As String
Open Pipe "fbc.exe" For Input As #1
Print "Output of fbc:"
Do While Not EOF(1)
   Line Input #1, text
   Print text
Loop

sleep
However this code that I first tried did not. (Same as suggested by axipher).

Code: Select all

Dim text As String
Open Pipe "C:\program files\FreeBASIC\fbc.exe" For Input As #1
Print "Output of fbc:"
Do While Not EOF(1)
   Line Input #1, text
   Print text
Loop
This fails at the first space character in the "c:\program files" directory name. Can an executable be prefixed with its path in for example "open pipe"? Maybe a bug in the parser? Or is there an include file to give FreeBASIC the ability to handle long directory names with spaces?
FreeBASIC now has less bugs than me!
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

Thanks for pointing this out. I've modified the Wiki page.
http://www.freebasic.net/wiki/wikka.php ... KeyPgChdir
If you find any more errors, don't be afraid to create a Wiki account for changing such things yourself.

With the Pipe example, I think the command text, at least in Windows, is in the format you'd type it into a command prompt. This allows you to add additional parameters, or type non-executable commands. e.g.

Code: Select all

Dim text As String
Open Pipe "dir" For Input As #1
Print "Output of dir:"
Do Until Eof(1)
   Line Input #1, text
   Print text
Loop
So, for path names with spaces, you could try enclosing it in quotes (note that two double-quotes ("") in a string gives a double-quote for you)

Code: Select all

Dim text As String
Open Pipe """C:\program files\FreeBASIC\fbc.exe""" For Input As #1
Print "Output of fbc:"
Do Until Eof(1)
   Line Input #1, text
   Print text
Loop
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Post by Richard »

It is clear that the external "command" is being automatically prefixed with the current directory, CurDir.
Effectivly we have (for DOS and Windows); Open pipe (CurDir & "\" & command) For Input As #1
This is not clearly documented.

Since the following works, but not once "C:\program files" becomes part of the "command" it seems that there may be a problem with the parsing of spaces in the command within the "open pipe" statement.

Code: Select all

Dim pathname As String = "C:\program files"
Dim result As Integer = ChDir(pathname)
If 0 <> result Then Print "error changing current directory to " & pathname & "." 

Dim text As String
Open Pipe "FreeBASIC\fbc.exe" For Input As #1
Print "Output of fbc:"
Do While Not EOF(1)
   Line Input #1, text
   Print text
Loop

sleep
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Post by Richard »

We overlapped. Yes I need to get a Wiki account so I can correct the odd typo. Thanks counting_pine for that solution.
Post Reply