I've been wanting to add some command-line interactivity to some of my programs, but I found that the console module's standard console output cannot be redirected: any attempt to do so results in a pseudo-console-output being sent to the console regardless, or no output being shown, while the "real" console output is empty. The same situation happens with the console module's standard error output, and it also happens when the "CON" file (console) is used for output.
What does work as intended is the CGI_write function from the CGI module.
Note that I am using thinbasicc.exe to interpret the scripts, not thinbasic.exe
This is the code I tested:
'cgiwrite.tbasic
'--------------------
#Bundle Type CONSOLE
Uses "cgi"
cgi_write("hello world")
'--------------------
'consolewriteline.tbasic
'--------------------
#Bundle Type CONSOLE
Uses "console"
Console_WriteLine("hello world")
'--------------------
'consolewritelineerror.tbasic
'--------------------
#Bundle Type CONSOLE
Uses "console"
Console_WriteLineerror("hello world")
'--------------------
'fileappend.tbasic
'--------------------
#Bundle Type CONSOLE
Uses "file"
FILE_Append("con","hello world")
'--------------------
'fileoutput.tbasic
'--------------------
#Bundle Type CONSOLE
Uses "file"
Dim afile As Number
afile = FILE_Open("CON","OUTPUT")
FILE_LinePrint(afile,"hello world")
FILE_Close(afile)
'--------------------
The code was tested as both thinBasic scripts and as bundled executables.
This is an additional file, "dummy.bat", used for reference purposes:
This is the test, a file called "tests.bat":
@echo off
c:\thinbasic\thinbasicc.exe "%cd%\cgiwrite.tbasic" > "%cd%\cgiwrite.txt" 2> "%cd%\cgiwrite2.txt"
c:\thinbasic\thinbasicc.exe "%cd%\consolewriteline.tbasic" > "%cd%\consolewriteline.txt" 2> "%cd%\consolewriteline2.txt"
c:\thinbasic\thinbasicc.exe "%cd%\consolewritelineerror.tbasic" > "%cd%\consoleerror.txt" 2> "%cd%\consoleerror2.txt"
c:\thinbasic\thinbasicc.exe "%cd%\fileappend.tbasic" > "%cd%\fileappend.txt" 2> "%cd%\fileappend2.txt"
c:\thinbasic\thinbasicc.exe "%cd%\fileoutput.tbasic" > "%cd%\fileoutput.txt" 2> "%cd%\fileoutput2.txt"
@cmd /c "%cd%\dummy.bat" > "%cd%\dummy.txt" 2> "%cd%\dummy2.txt"
if exist "%cd%\cgiwrite.exe" ("%cd%\cgiwrite.exe" > "%cd%\cgiwriteexe.txt" 2>"%cd%\cgiwriteexe2.txt" )
if exist "%cd%\consolewriteline.exe" ("%cd%\consolewriteline.exe" > "%cd%\consolewritelineexe.txt" 2>"%cd%\consolewritelineexe2.txt" )
if exist "%cd%\consolewritelineerror.exe" ("%cd%\consolewritelineerror.exe" > "%cd%\consoleerrorexe.txt" 2>"%cd%\consoleerrorexe2.txt" )
if exist "%cd%\fileappend.exe" ("%cd%\fileappend.exe" > "%cd%\fileappendexe.txt" 2>"%cd%\fileappendexe2.txt" )
if exist "%cd%\fileoutput.exe" ("%cd%\fileoutput.exe" > "%cd%\fileappendexe.txt" 2>"%cd%\fileappendexe2.txt" )
dir *.txt
Note that the "2>" is meant to redirect error output, while ">" redirects normal output.
This is the output of the test:
hello world
hello world
'[irrelevant lines omitted]
17/11/2017 19:07 11 cgiwrite.txt
17/11/2017 19:07 0 cgiwrite2.txt
17/11/2017 19:07 11 cgiwriteexe.txt
17/11/2017 19:07 0 cgiwriteexe2.txt
17/11/2017 19:07 0 consoleerror.txt
17/11/2017 19:07 0 consoleerror2.txt
17/11/2017 19:07 0 consoleerrorexe.txt
17/11/2017 19:07 0 consoleerrorexe2.txt
17/11/2017 19:07 0 consolewriteline.txt
17/11/2017 19:07 0 consolewriteline2.txt
17/11/2017 19:07 0 consolewritelineexe.txt
17/11/2017 19:07 0 consolewritelineexe2.txt
17/11/2017 19:07 13 dummy.txt
17/11/2017 19:07 0 dummy2.txt
17/11/2017 19:07 0 fileappend.txt
17/11/2017 19:07 0 fileappend2.txt
17/11/2017 19:07 0 fileappendexe.txt
17/11/2017 19:07 0 fileappendexe2.txt
17/11/2017 19:07 0 fileoutput.txt
17/11/2017 19:07 0 fileoutput2.txt
I haven't yet figured out exactly which programs are responsible for the two "hello world"s in the test's output.
After reading this thread, I changed the scripts' extensions to .thinbasicc (and changed the test accordingly), just to be sure, but the test's output was still the same.
Bookmarks