How to capture screen output?

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
wergosum
Posts: 6
Joined: Jan 03, 2011 15:41

How to capture screen output?

Post by wergosum »

Hello!

The following line in a programme

Exec ("gdallocationinfo"," -valonly -wgs84 " & weatherfile & " 125.25 37.7 ")

outputs a value (say, 46!) to the console.

How can I "import" or "intercept" this value and use it in my programme?

Many thanks!
Roland Chastain
Posts: 1002
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: How to capture screen output?

Post by Roland Chastain »

Hello! You can use the OPEN PIPE command.

There is an example here.
wergosum
Posts: 6
Joined: Jan 03, 2011 15:41

Re: How to capture screen output?

Post by wergosum »

Many thanks, Roland. This is exactly what I was looking for!

I just tried this

DIM AS STRING GDALLOCATIONINFO, LIN, WEATHERFILE="./ECMWF_dekads/int_1989-2015/Rain/int_19890101rf.img"
DIM AS BYTE CONSOLE
CONSOLE=FREEFILE
GDALLOCATIONINFO = "gdallocationinfo -valonly -wgs84 " & weatherfile & " 125.25 37.7 "
OPEN PIPE GDALLOCATIONINFO FOR INPUT AS #CONSOLE
LINE INPUT #CONSOLE, LIN
PRINT LIN

which produces the desired result.

Is there a risk that I won't catch all the output if I don't use the DO .... LOOP UNTIL EOF() ?

René
Roland Chastain
Posts: 1002
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: How to capture screen output?

Post by Roland Chastain »

wergosum wrote:Is there a risk that I won't catch all the output if I don't use the DO .... LOOP UNTIL EOF() ?
If the answer has only one line, I imagine that it should work without looping.

Happy Easter!
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to capture screen output?

Post by fxm »

Yes but after the reading of the first string, it is better to close the process:
Close #CONSOLE
wergosum
Posts: 6
Joined: Jan 03, 2011 15:41

Re: How to capture screen output?

Post by wergosum »

Hello Roland & fxm

I tried all the option, and it is indeed necessary to

CLOSE #CONSOLE

as otherwise the same value is returned indefinitely. As to WHILE-WEND loop Vs no loop, this is pretty indifferent as the execution time remains the same. I wonder why the examples include that loop.

If you are from an "Easter egg culture", enjoy the eggs!

R.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How to capture screen output?

Post by MrSwiss »

wergosum wrote:I wonder why the examples include that loop.
Well, because it is the only truely 'safe way', to do it since,
it has to 'work' independently of the amount of returned strings ...

Example: a 'dir' / 'ls' shell-statement (amount of return is considered: 'unknown')
wergosum
Posts: 6
Joined: Jan 03, 2011 15:41

Re: How to capture screen output?

Post by wergosum »

Thanks, MrSwiss

Another related question

Is there any specific reason why the example that Roland pointed me to uses constants?

#IFDEF __FB_UNIX__
CONST TEST_COMMAND = "ls *"
#ELSE
CONST TEST_COMMAND = "dir *.*"
#ENDIF

[etc]

OPEN PIPE TEST_COMMAND FOR INPUT AS #nr

The command works fine with a string variable too.

R
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How to capture screen output?

Post by MrSwiss »

Constant(s) are by definition 'global', while 'ordinary' *variables, are not.
'global' = access, from procedures as well, as module level code

*Variable exception: 'Dim Shared', which is generally not recommended!
wergosum
Posts: 6
Joined: Jan 03, 2011 15:41

Re: How to capture screen output?

Post by wergosum »

OK, thanks. This makes sense too if one wants to compile for windows or linux...
Post Reply