Recent

Author Topic: AssignStream - Unix & Windows  (Read 5927 times)

WalterK

  • New Member
  • *
  • Posts: 48
AssignStream - Unix & Windows
« on: July 12, 2011, 02:14:54 pm »
Is there an equivalent to AssignStream for Windows?  Neither the Unix or OldLinux libraries exist there.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12721
  • FPC developer.
Re: AssignStream - Unix & Windows
« Reply #1 on: July 12, 2011, 05:09:40 pm »
The TProcess class in unit Process does this. It also works for *nix, and is generally used a lot more. Search for examples in the wiki.

WalterK

  • New Member
  • *
  • Posts: 48
Re: AssignStream - Unix & Windows
« Reply #2 on: July 12, 2011, 06:18:29 pm »
It looks a bit more complicated than I was hoping for, but also like it will do the job.  I can always follow the example in the wiki:
http://wiki.lazarus.freepascal.org/Executing_External_Programs#TProcess

Thanks!

WalterK

  • New Member
  • *
  • Posts: 48
Re: TProcess.Input.Write note
« Reply #3 on: July 18, 2011, 06:36:11 pm »
I thought I'd comment that I had to add carriage returns to the output lines or else the executed program just saw a stream of characters (rather than terminated lines) without them.

Fwiw, I'm testing on a PowerPC Mac - maybe it will run differently on Windows.  (I'll get there eventually...)

  while (not eof(tf)) do
     begin
     readln (tf, aline);
{$ifdef unix}
     aline := aline + char(10);
{$else}
     aline := aline + char(13)+char(10);
{$endif}
     P.Input.Write (aline[1],Length(aline));
     end;
  P.CloseInput();

Anyway, it's working now.  Thanks.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: AssignStream - Unix & Windows
« Reply #4 on: July 18, 2011, 07:31:54 pm »
Quote
I thought I'd comment that I had to add carriage returns to the output lines or else the executed program just saw a stream of characters (rather than terminated lines) without them.
Well... you're using ReadLn to get the input, so it's normal that you don't get the line ending. Instead of using ifdef, a constant LineEnding in System unit has been set to platform's native line ending format.

WalterK

  • New Member
  • *
  • Posts: 48
Re: AssignStream - Unix & Windows
« Reply #5 on: July 21, 2011, 03:41:51 pm »
Yes, I see about why eols are dropped.  I guess I'd really like to know how to use the standard readln & writeln for i/o.  I had written the code to use sockets, files, and pipes and was using readln and writeln to put and get data.  Now, with TProcess streams, I can't seem to do that.  Passing the Input.Handle to writeln will compile but not run; it bombs with error 103 (while the Input.Write function works).  I'll suppose the handle is not the same as what writeln expects... Is there a way to use standard i/o?

TIA.

 

TinyPortal © 2005-2018