How to capture screen output?

For other topics related to the FreeBASIC project or its community.
wergosum
Posts: 6
Joined: Jan 03, 2011 15:41

How to capture screen output?

Postby wergosum » Mar 31, 2018 19:32

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: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Re: How to capture screen output?

Postby Roland Chastain » Mar 31, 2018 20:00

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?

Postby wergosum » Mar 31, 2018 20:24

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: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Re: How to capture screen output?

Postby Roland Chastain » Apr 01, 2018 12:03

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
Posts: 8702
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: How to capture screen output?

Postby fxm » Apr 01, 2018 12:48

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?

Postby wergosum » Apr 01, 2018 12:57

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: 2934
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How to capture screen output?

Postby MrSwiss » Apr 01, 2018 13:21

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?

Postby wergosum » Apr 01, 2018 17:31

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: 2934
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How to capture screen output?

Postby MrSwiss » Apr 01, 2018 17:59

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?

Postby wergosum » Apr 02, 2018 3:58

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

Return to “Community Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest