Recent

Author Topic: How to write buffer to console  (Read 4340 times)

ydd

  • Jr. Member
  • **
  • Posts: 82
How to write buffer to console
« on: May 31, 2021, 07:29:11 pm »
I have ABuf, ALen

I know that writing to console uses Output (same as StdOut), so I need something like:

Code: Pascal  [Select][+][-]
  1. BlockWrite(Output, ABuf, ALen);

should be cross platform - at least Windows, Linux, x86, ARM

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: How to write buffer to console
« Reply #1 on: May 31, 2021, 10:10:46 pm »
Iterate over it using Write()/WriteLn() possibly with the help of Format(). That's about as much as can be said given the lack of any information regarding what you actually expect to see.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

cdbc

  • Hero Member
  • *****
  • Posts: 2814
    • http://www.cdbc.dk
Re: How to write buffer to console
« Reply #2 on: May 31, 2021, 10:51:45 pm »
Hi
If the buffer contains pure text with linefeeds, you could use a TStringlist.Text and writeln the individual lines to console, should work on all platforms...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: How to write buffer to console
« Reply #3 on: May 31, 2021, 11:04:30 pm »
If the buffer contains pure text with linefeeds, you could use a TStringlist.Text and writeln the individual lines to console, should work on all platforms...
Regards Benny

Possibly, but allowing for the cross-platform aspect I'm concerned about the OP's expectations regarding line breaks etc.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to write buffer to console
« Reply #4 on: May 31, 2021, 11:52:25 pm »
This seems to work on Windows. Give it a try and see if it works on Linux:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Classes, SysUtils
  10.   { you can add units after this };
  11.  
  12. var
  13.   hs:THandleStream;
  14.   s:string='';
  15.   buf:pchar;
  16.   i: Integer;
  17. begin
  18.   for i := 1 to 10 do
  19.     s:=s+'Test: '+IntToStr(i)+LineEnding;
  20.  
  21.   hs:=THandleStream.Create(TextRec(Output).Handle);
  22.   try
  23.     buf:=@s[1];
  24.     hs.WriteBuffer(buf^,Length(s));
  25.   finally
  26.     hs.Free;
  27.   end;
  28.  
  29.   readLn;
  30. end.

Edit:
Just tried it on Android and it worked.
« Last Edit: June 01, 2021, 01:12:50 am by engkin »

ydd

  • Jr. Member
  • **
  • Posts: 82
Re: How to write buffer to console
« Reply #5 on: June 01, 2021, 01:23:10 am »
This seems to work on Windows. Give it a try and see if it works on Linux: ..

actually thank you for your idea, I updated it a little and it works on Windows & Linux:

Code: Pascal  [Select][+][-]
  1. procedure WriteBuf(ABuf: Pointer; ALen: Integer);
  2. begin
  3.   FileWrite(TextRec(Output).Handle, ABuf^, ALen);
  4. end;

AdrianHamilton

  • Newbie
  • Posts: 1
Re: How to write buffer to console
« Reply #6 on: June 03, 2021, 01:33:53 pm »
Hi, for some reason it's not working for me.... I did exactly as you did and nothing changed! What could be the problem?

jamie

  • Hero Member
  • *****
  • Posts: 7765
Re: How to write buffer to console
« Reply #7 on: June 03, 2021, 05:15:39 pm »
Try the System.StdOutPutHandle instead.

The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018