Recent

Author Topic: How do I write binary data to stdout under Linux?  (Read 4810 times)

hakelm

  • Full Member
  • ***
  • Posts: 153
How do I write binary data to stdout under Linux?
« on: September 24, 2018, 10:07:30 am »
I would like to write binary data to stdout under Linux so I can pipe the output of my program into another.
Now stdout is a text file, is there any way to retype it to for example file of double?
It would also be interesting to know how to read binary data from stdin.
H

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: How do I write binary data to stdout under Linux?
« Reply #1 on: September 24, 2018, 10:15:55 am »
You can get the OS handle of a pascal FILE or TEXT using getfilehandle

Thaddy

  • Hero Member
  • *****
  • Posts: 14198
  • Probably until I exterminate Putin.
Re: How do I write binary data to stdout under Linux?
« Reply #2 on: September 24, 2018, 10:31:54 am »
As you yourself wrote "pipe the output".... Well then, use pipes, these are there for just that and more...
There's a good pipes example in the standard distribution. Your approach is likely to fail at least on some platforms because a console may filter one or more byte values anyway.
A true pipe won't do that.
anyway, following Marco's hint a small example that may or may not work:
Code: Pascal  [Select][+][-]
  1. program streamtest;
  2. {$ifdef fpc}{$mode delphi}{$H+}{$endif}
  3. uses classes, sysutils;
  4. var
  5.  stream:THandleStream;
  6.  buffer:array[0..5] of char = 'testme'; // any data
  7. begin
  8.  stream := THandleStream.Create(GetFileHandle(output));
  9.  try
  10.    stream.write(buffer,length(buffer));
  11.  finally
  12.    stream.free;
  13.  end;
  14. end.
This example works on windows and linux, but note my remarks on console/terminal filtering certain byte values.
« Last Edit: September 24, 2018, 11:28:40 am by Thaddy »
Specialize a type, not a var.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How do I write binary data to stdout under Linux?
« Reply #3 on: September 24, 2018, 02:31:04 pm »
It may be also worth looking into how to set the console to raw mode, to try to avoid the filtering to which Thaddy refers. I'll have to search how that's done later; I don't rightly remember ATM.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: How do I write binary data to stdout under Linux?
« Reply #4 on: September 24, 2018, 02:36:41 pm »
termio unit. See video/textmode IDE for details.

hakelm

  • Full Member
  • ***
  • Posts: 153
Re: How do I write binary data to stdout under Linux?
« Reply #5 on: September 24, 2018, 03:03:29 pm »
Thanks a lot.
THandleStream does the trick, all my testbytes have so far been received correctly.
I couldn't get popen to work since I can't figure out what to use as the second parameter 'Prog'.
H

Thaddy

  • Hero Member
  • *****
  • Posts: 14198
  • Probably until I exterminate Putin.
Re: How do I write binary data to stdout under Linux?
« Reply #6 on: September 24, 2018, 06:03:54 pm »
Actually the example I gave is in fact a simplified example of TOutputPipeStream from the pipes unit (package fcl-process).
Both TInputPipeStream and TOutputPipeStream are provided. I recommend using them instead of the example I gave you.
Only change is:
Code: Pascal  [Select][+][-]
  1. {$ifdef fpc}{$mode delphi}{$H+}{$endif}
  2. uses pipes,sysutils;
  3. var
  4.  stream:TOutputPipeStream;
  5.  buffer:array[0..5] of char = 'testme';
  6. begin
  7.  stream := TOutputPipeStream.Create(GetFileHandle(output));
  8.  try
  9.    stream.write(buffer,length(buffer));
  10.  finally
  11.    stream.free;
  12.  end;
  13. end.
Since pipes use the same mechanism I proposed above, there seems to be no direct risk on loosing data on major platforms.
The use ot the pipes unit classes give a little more protection against mistakes.
Specialize a type, not a var.

 

TinyPortal © 2005-2018