redirect console output to file or pipe

Share your advanced PureBasic knowledge/code with the community.
Insomniac
New User
New User
Posts: 6
Joined: Sun Apr 27, 2003 12:28 am
Location: New Zealand

redirect console output to file or pipe

Post by Insomniac »

Code updated for 5.20+. Built-in console command now feature this as well.

For the benefit of Ajm ( he's threating to use languages other than purebasic if the console commands don't improve - gasp!) here's a quick example I created by expanding wichtel's scrolling console tip into procedures.

Allows console program output to be sent to a file ( eg. myprogram > myfile.txt ) or to a pipe (e.g. myprogram | more ) or to the screen of course.

Code: Select all


; allows console output to scroll and be redirected to file and pipe
; expanded version of wichtel's scrolling console example
; does not support ansi ( ie. color) or unicode 
; as writefile is used in place of writeconsole - see api doc 

Global stdout.l ; stdout handle

Procedure.l MyOpenConsole()

  stdout = GetStdHandle_(#STD_OUTPUT_HANDLE) ; check if we already have a console ie. command prompt
  If stdout = #INVALID_HANDLE_VALUE ; no we don't have existing console
    If allocconsole_() ;attempt to open a new one
      stdout=GetStdHandle_(#STD_OUTPUT_HANDLE) 
    Else
      ProcedureReturn #FALSE
    EndIf
  EndIf
 
  ProcedureReturn #TRUE     
EndProcedure
  
  
Procedure.l MyPrintN(Stringparm.s)
  
  written.l
  msg$ = Stringparm + Chr(13) + Chr(10)
  size.l=Len(msg$) 
  res = writefile_(stdout,@msg$,size, @written, #NULL)
  Delay(30)
    
  ProcedureReturn res
EndProcedure

Procedure.l MyCloseConsole()

  Delay(1000) 
  res = freeconsole_() 

  ProcedureReturn res
EndProcedure
  
; Main program
 
If MyOpenConsole()
 
  For i=1 To 100 

  MyPrintN("hello world "+Str(i)) 

  Next i 
  
  MyCloseConsole()
  
EndIf
Wichtel's scrolling console tip and other console related info was here:
viewtopic.php?p=30314

Regards

Insomniac
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

he's threating to use languages other than purebasic if the console commands don't improve
8O ack!...

P.S. nice example :)
--Kale

Image
RJP Computing
Enthusiast
Enthusiast
Posts: 202
Joined: Sun Apr 27, 2003 4:44 am
Location: Michigan, USA
Contact:

Post by RJP Computing »

I must be missing something but I don't get any output at all. I have to be missing something.
Please help
-Ryan
RJP Computing

Ubuntu 8.10/WinXP, AMD Athlon 64 3000+, 1000MB RAM, AC 97 Audio, nVidia GeForce 7600GT 512MB
User avatar
Ajm
Enthusiast
Enthusiast
Posts: 242
Joined: Fri Apr 25, 2003 9:27 pm
Location: Kent, UK

Post by Ajm »

I had the same problem,
you have to compile it as a console application.

So far it seems to work fine for what I want.
Regards

Andy

Image
Registered PB & PureVision User
RJP Computing
Enthusiast
Enthusiast
Posts: 202
Joined: Sun Apr 27, 2003 4:44 am
Location: Michigan, USA
Contact:

Post by RJP Computing »

that did it. Thanks
-Ryan
RJP Computing

Ubuntu 8.10/WinXP, AMD Athlon 64 3000+, 1000MB RAM, AC 97 Audio, nVidia GeForce 7600GT 512MB
Post Reply