• Print

Author Topic: Reading input from STDIN and writing to STDOUT  (Read 6035 times)

DSMan195276

  • Hero Member
  • *****
  • Posts: 2112
  • Yes
Reading input from STDIN and writing to STDOUT
« on: February 10, 2012, 06:26:51 am »
stdin/stdout are the standard input/output for C, and though QB64 doesn't use them (It uses SDL's to get keyboard input and then writes it to the SDL window) they're very helpful for a command-line interface (Since stdin and stdout read and write directly to the command line). It's even more helpful in Linux because using them allows QB64 to be sent data VIA pipes and return data to pipes.

To make QB64 read from stdin and write to stdout we can use this small wrapper:

Code: [Select]
#include <cstdio>

int getchar_stdin(){
unsigned char getChar;
getChar = fgetc (stdin);
return (int)getChar;
}

void write_string(char * s){
printf("%s", s);
return ;
}

And call it accordingly:

Code: [Select]
DECLARE LIBRARY "std_wrapper"
    FUNCTION getchar_stdin& ()
    SUB write_string (s AS STRING)
END DECLARE

write_string "Hello"
DO WHILE c <> 255
    c = getchar_stdin&
    PRINT CHR$(c);
LOOP
END

If you use this program it won't do anything unless you run it from the command line. It seems the SDL hang's up stdout, so the "Hello" won't appear until after the program closes. Because of this it's not useful for printing out debug info if the program doesn't make it to the end. I'm not sure there is any way to fix that. It is helpful though since you can then send output from QB64 either to the command-line, or pipe it to another program.

As for stdin, it reads one character at a time (Like INKEY$). Unlike inkey though, when the buffer is empty it will return CHR$(255), so you'll have to check for that in your program.

If you want to send data to stdin on Linux, you can do so a few ways. the most basic are:
Code: [Select]
ls | ./program
./program < "filename.txt"

Most Linux users who use the command line will be familiar with these. The first one pipes the output of the 'ls' command to stdin. The second one sends the contents of "filename.txt" to stdin.

Hope that's helpful,

Matt
"Cast your cares on the Lord and he will sustain you; he will never let the righteous be shaken" -- Psalm 55:22
QB64 Linux Installer

Clippy

  • Hero Member
  • *****
  • Posts: 19011
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
Re: Reading input from STDIN and writing to STDOUT
« Reply #1 on: February 10, 2012, 06:50:58 am »
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 SDL: QB64v0954-win15.7z

DSMan195276

  • Hero Member
  • *****
  • Posts: 2112
  • Yes
Re: Reading input from STDIN and writing to STDOUT
« Reply #2 on: February 10, 2012, 07:39:57 am »
Quote from: Clippy on February 10, 2012, 06:50:58 am
http://qb64.net/wiki/index.php?title=C_Libraries#Console_Window



That's actually a different idea. That reroutes stdout to a different console window, and doesn't bother doing anything with stdin.

My code here sends data to stdout on the same console that was used to open the program. My write_string sub is basically the equivalent of using the c_print sub that Unseen wrote without first using the "consoleWindow" sub.

Matt
"Cast your cares on the Lord and he will sustain you; he will never let the righteous be shaken" -- Psalm 55:22
QB64 Linux Installer

Clippy

  • Hero Member
  • *****
  • Posts: 19011
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
Re: Reading input from STDIN and writing to STDOUT
« Reply #3 on: February 10, 2012, 08:00:19 am »
I just thought that was relevant.

C_Print doesn't display on its own either. I tried just using it with AllocConsole directly and nothing happens.
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 SDL: QB64v0954-win15.7z

DSMan195276

  • Hero Member
  • *****
  • Posts: 2112
  • Yes
Re: Reading input from STDIN and writing to STDOUT
« Reply #4 on: February 10, 2012, 08:17:55 am »
Quote from: Clippy on February 10, 2012, 08:00:19 am
I just thought that was relevant.

C_Print doesn't display on its own either. I tried just using it with AllocConsole directly and nothing happens.

It was, I actually didn't know that was in the Wiki until you posted the link to it.

Did you run it from the CMD and check the window after the program exited? stdout hangs while the program is running, but if you run it from the CMD it should display what you sent to it after the program exits. If you use AllocConsole to open a new console window (Because the program wasn't run from the CMD) then you have to use the freopen() line to redirect stdout to the new console that was opened.

Also, I think it should be noted in the Wiki that AllocConsole() is Windows specific (I'm currently looking into a crossplatform and/or a Linux alternative that will spawn a terminal window, but sadly no easy solutions yet.)

Matt
"Cast your cares on the Lord and he will sustain you; he will never let the righteous be shaken" -- Psalm 55:22
QB64 Linux Installer

Clippy

  • Hero Member
  • *****
  • Posts: 19011
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
Re: Reading input from STDIN and writing to STDOUT
« Reply #5 on: February 10, 2012, 09:11:33 am »
Are SetCurrentDirectory and system also Windows specific?
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 SDL: QB64v0954-win15.7z

DSMan195276

  • Hero Member
  • *****
  • Posts: 2112
  • Yes
Re: Reading input from STDIN and writing to STDOUT
« Reply #6 on: February 10, 2012, 11:25:18 am »
Quote from: Clippy on February 10, 2012, 09:11:33 am
Are SetCurrentDirectory and system also Windows specific?

'system' is not, but 'SetCurrentDirectory' is. 'system' is a command defined in stdlib.h, and 'system' is C's equivalent of SHELL, it just passes the string you send it to the OS's command processor. SetCurrentDirectory is part of the Windows API, so naturally it's Windows specific.

Matt
"Cast your cares on the Lord and he will sustain you; he will never let the righteous be shaken" -- Psalm 55:22
QB64 Linux Installer

Clippy

  • Hero Member
  • *****
  • Posts: 19011
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
Re: Reading input from STDIN and writing to STDOUT
« Reply #7 on: February 10, 2012, 11:36:31 am »
Will system work in Linus and Mac?
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 SDL: QB64v0954-win15.7z

DSMan195276

  • Hero Member
  • *****
  • Posts: 2112
  • Yes
Re: Reading input from STDIN and writing to STDOUT
« Reply #8 on: February 10, 2012, 11:44:14 am »
Quote from: Clippy on February 10, 2012, 11:36:31 am
Will system work in Linus and Mac?

Yes it will.

Matt
"Cast your cares on the Lord and he will sustain you; he will never let the righteous be shaken" -- Psalm 55:22
QB64 Linux Installer

Clippy

  • Hero Member
  • *****
  • Posts: 19011
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
Re: Reading input from STDIN and writing to STDOUT
« Reply #9 on: February 10, 2012, 02:05:22 pm »
Now all we need is something that can make a console winder. Or don't they like winders?
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 SDL: QB64v0954-win15.7z

Galleon

  • Administrator
  • Hero Member
  • *****
  • Posts: 5468
  • QB Forever
Re: Reading input from STDIN and writing to STDOUT
« Reply #10 on: June 28, 2012, 06:40:30 am »
For the record you can do this:
Code: [Select]
$CONSOLE
_DEST _CONSOLE
PRINT "Hello"
INPUT "What isn't your name? >", not_name$
PRINT "Your name is not " + not_name$
_DEST 0

PS. A bot dug up this post, but my reply seemed relevant so I'm leaving it here.
Something old... Something new... Something borrowed... Something blue...

DSMan195276

  • Hero Member
  • *****
  • Posts: 2112
  • Yes
Re: Reading input from STDIN and writing to STDOUT
« Reply #11 on: June 28, 2012, 11:56:35 pm »
Actually Galleon, I'm not sure that works the same? My code here will read input from STDIN which I don't believe is possible just using _DEST _CONSOLE (Though I'm not sure I've tried it to be honest). If I send something to STDIN via a Pipe (or ortherwise) how would I go about grabbing it using $CONSOLE, _DEST_CONSOLE, etc.? I don't believe it's possible since the current setup doesn't do anything about STDIN, just writing to STDOUT.

Matt
"Cast your cares on the Lord and he will sustain you; he will never let the righteous be shaken" -- Psalm 55:22
QB64 Linux Installer

Luis Babboni

  • Full Member
  • ***
  • Posts: 108
Re: Reading input from STDIN and writing to STDOUT
« Reply #12 on: March 15, 2015, 10:15:49 am »
Its there anybody still remember this topic?

I need some way to communicate my future program (a chess engine) with a GUI that uses "pipes" whatelse that this are  ::)
In talkchess.com, where for the moment do not appears nobody that knows QB64 they explain me that my first QB64 attemp fails, as it is, cause the usual Print do not send data inmediatly but store it in buffered... at least is what i understood.... so I need something like flush command in C++
Is this discussion here about stdin and stdout something related with it?
I tried DSman195276 code but is wrote in C++, I´m right?
What I need to do to make its wrks with QB64?  :-[


Luis Babboni

  • Full Member
  • ***
  • Posts: 108
Re: Reading input from STDIN and writing to STDOUT
« Reply #13 on: April 06, 2015, 02:46:28 am »
Again..... anyone could help me with this?

Thanks.

unseenmachine

  • Hero Member
  • *****
  • Posts: 3542
  • Make the Game not the ENGINE!!!
Re: Reading input from STDIN and writing to STDOUT
« Reply #14 on: April 06, 2015, 05:42:43 am »
Quote
I tried DSman195276 code but is wrote in C++, I´m right?
Yes and no. the first bit of code is a wrapper written in C++, the second bit of code adds the functions in the wrapper to QB64 and shows you how to use them.

Save the first bit of code as std_wrapper.h and put it in your QB64 folder, then run matts second bit of code in QB64.

Unseen
UnseenGDK Download : http://dl.dropbox.com/u/8822351/UnseenGDK.bm
GDK Tutorial : http://dl.dropbox.com/u/8822351/UnseenGDK_Tutorial.doc
VQB02 : https://www.dropbox.com/s/zz5ve8e75gcie8z/VQB02.bm?dl=0

  • Print