Recent

Author Topic: can someone straighten me out with this code?  (Read 1085 times)

toby

  • Sr. Member
  • ****
  • Posts: 252
can someone straighten me out with this code?
« on: February 11, 2023, 10:42:55 pm »
could someone straightem me out with this code

fs.write ? vs fs.read
fs.fsize?
fs.position?

where does the writeln go?  i would like to recover it ???
for j := 1 to 30 do write(j); // where is this going?



Code: Pascal  [Select][+][-]
  1. program stdouty;
  2.  
  3. //{$mode objfpc}{$H+}
  4.  
  5. uses sysutils, classes, baseunix;
  6.  
  7. //var fs : tfilestream;
  8. var fs : thandlestream;
  9.     oldoutputhandle : thandle;
  10.     olderrorhandle : thandle;
  11.     i, j : longint;
  12.     e : array[1..4] of longint;
  13.  
  14. begin
  15.  
  16. for i := 1 to 4 do e[i] := 2;
  17.  
  18. // save original handles
  19. oldoutputhandle := fpdup(textrec(stdout).handle);
  20. olderrorhandle := fpdup(textrec(stderr).handle);
  21.  
  22. //fs := tfilestream.create('stdout.tmp', fmopenreadwrite or fmcreate);
  23. fs := thandlestream.create(stdoutputhandle);
  24.  
  25. // assign output and error handles to a single file
  26. fpdup2(fs.handle, stderrorhandle);
  27. fpdup2(fs.handle, stdoutputhandle);
  28.  
  29. for j := 1 to 30 do write(j); // where is this going?
  30.  
  31. writeln('fs.position : ', fs.position);
  32. fs.position := 0;
  33. writeln('fs.position : ', fs.position);
  34. writeln('fs.size : ', fs.size);
  35. writeln(fs.write(e, 4));
  36. for i := 1 to 4 do writeln('i : ', i, '  e[', e[i], ']');
  37.  
  38. fs.free;
  39.  
  40. // restore original handles
  41. fpdup2(oldoutputhandle, textrec(stdout).handle);
  42. fpdup2(olderrorhandle , textrec(stderr).handle);
  43.  
  44. end.
  45.  
« Last Edit: February 11, 2023, 10:45:38 pm by Martin_fr »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9913
  • Debugger - SynEdit - and more
    • wiki
Re: can someone straighten me out with this code?
« Reply #1 on: February 11, 2023, 10:46:10 pm »
Please use [ code=pascal ]  [ /code ]  tags. I added them for you.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2097
  • Fifty shades of code.
    • Delphi & FreePascal
Re: can someone straighten me out with this code?
« Reply #2 on: February 11, 2023, 11:15:40 pm »
Please use [ code=pascal ]  [ /code ]  tags. I added them for you.
Thank you, but even with, it makes that code not looking better  :D
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: can someone straighten me out with this code?
« Reply #3 on: February 12, 2023, 01:03:15 am »
Please use [ code=pascal ]  [ /code ]  tags. I added them for you.
Thank you, but even with, it makes that code not looking better  :D

Yeah, an auto format option in the forum would be good... However, that can be problematic if the code is not valid...

Joanna

  • Hero Member
  • *****
  • Posts: 771
Re: can someone straighten me out with this code?
« Reply #4 on: February 12, 2023, 02:33:49 am »
Quote
     e : array[1..4] of longint;
 
begin
 
for i := 1 to 4 do e := 2;
This might not answer your question but I have a suggestion for making your coding style more robust  ;D
 
Code: Pascal  [Select][+][-]
  1.  For I:= 1 to high(e) do
  2.       E[I]:= 2;

Quote
for j := 1 to 30 do write(j); // where is this going?
Have you tried placing that line in other parts of code or using writeln to see what happens?
« Last Edit: February 12, 2023, 02:41:35 am by Joanna »
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

TRon

  • Hero Member
  • *****
  • Posts: 2540
Re: can someone straighten me out with this code?
« Reply #5 on: February 12, 2023, 04:57:53 am »
could someone straightem me out with this code
I've copied your code, rewrote some stuff and added some comments to get you a bit more familiar with what is happening.

Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   sysutils, classes, baseunix;
  7.  
  8. var
  9.   hs : thandlestream;  // renamed to hs to indicate HandleStream (and not FileStream)
  10.   oldoutputhandle : thandle;
  11.   olderrorhandle : thandle;
  12.   i, j : longint;
  13.   e : array[1..4] of longint;
  14.   c : array[1..4] of char = 'abcd'; // additional buffer for testing
  15.   s : string; // additional string s for an additional example
  16. begin
  17.   // corrected your loop to avoid using wrong indices
  18.   for i := low(e) to high(e) do e[i] := 2;
  19.  
  20.   // duplicate original stdout & stderr handles for safekeeping
  21.   oldoutputhandle := fpdup(textrec(stdout).handle);
  22.   olderrorhandle  := fpdup(textrec(stderr).handle);
  23.  
  24.   // Create a handlestream for stdoutput
  25.   hs := thandlestream.create(stdoutputhandle);
  26.  
  27.   // copy/assign handlestream's handle to stderrorhandle and stdoutputhandle
  28.   fpdup2(hs.handle, stderrorhandle);
  29.   fpdup2(hs.handle, stdoutputhandle);
  30.  
  31.   // write values of j to /stdout/
  32.   for j := 1 to 30
  33.     do write(j); // Q: where is this going? A: to stdout
  34.   writeln;
  35.  
  36.   // additional example that uses handlestream to write something to stdout
  37.   for j := 1 to 30 do
  38.   begin
  39.     s := j.ToString;
  40.     hs.write(s[1], Length(s));
  41.   end;
  42.   writeln;
  43.  
  44.   // there is no use calling hs.size and hs.position as afaik all data written
  45.   // to handlestream is directly emitted to stdout as handlestream is not buffered.
  46.   // Handlestream is initialized in such a way that it simply wraps around
  47.   // stdout so that you can use stream class functionality to write to stdout.
  48.   writeln('hs.position : ', hs.position);
  49.   hs.position := 0;
  50.   writeln('hs.position : ', hs.position);
  51.   writeln('hs.size : ', hs.size);
  52.  
  53.   // the following code-line in your source puzzled me a bit as it did not
  54.   // output what I expected. It should output 4 times a decimal value of 2
  55.   // (note: not the ascii representation of the value/number 2) and the
  56.   // length of the number of bytes written (which is 4) to the terminal.
  57.   // Instead, it only printed the number 4 for me.
  58.   // From that I have to conclude that either the terminal is smart enough to
  59.   // ignore the 4 decimal values of 2 or the write implementation filters out
  60.   // any characters that are not in "printable" range.
  61.  
  62.   //  writeln(hs.write(e, 4));
  63.  
  64.   // In order to see the same line in a "working" manner, i've adapted it to
  65.   // emit characters instead and which indeed show the expected result.
  66.   writeln(hs.write(c, length(c)));
  67.  
  68.   // corrected your loop to avoid using wrong indices and added some proper
  69.   // output that imho makes more sense.
  70.   for i := low(e) to high(e)
  71.     do writeln('e[', i, '] = ', e[i]);
  72.  
  73.   hs.free;
  74.  
  75.   // restore original handles
  76.   fpdup2(oldoutputhandle, textrec(stdout).handle);
  77.   fpdup2(olderrorhandle , textrec(stderr).handle);
  78. end.
  79.  

Quote
fs.write ? vs fs.read
The handle you wrapped around is stdout. Handlestream adds stream class functionality. Because the handlestream class is 'connected' by the handle to stdout you can only use the write functionality.

Quote
fs.fsize?
fs.position?
See comments in code.

Quote
where does the writeln go?
to stdout

Quote
i would like to recover it ???
With handlestream like you wrapped it in your code around stdout: you don't.

Handlestream is only a wrapper around a stream and as such an abstract class. Descendants of thandlestream are required to add actual/additional functionality.

Having said that, it can be used as in your code to add stream functionality to a handle which can be very convenient if you want to use streams.

Quote
for j := 1 to 30 do write(j); // where is this going?
Again, stdout.

Everything in the source-code that has the word write in it writes the content to stdout.

toby

  • Sr. Member
  • ****
  • Posts: 252
Re: can someone straighten me out with this code?
« Reply #6 on: February 12, 2023, 08:59:26 pm »
Tron thank you ... and you must know where this headed ....

since the tfilestream with fpdup can redirect apl_exec stdout to a file

and with thandlestream the writeln stdout can be put into a string from the hs.write

var s1 : ansistring;

s1 := '';
writeln('hs.write(s[1], Length(s));');
// additional example that uses handlestream to write something to stdout
for j := 1 to 30 do
  begin
  s := j.ToString;
  hs.write(s[1], Length(s));
  s1 := s1 + s;
  end;
writeln;

writeln('length(s1) : ', length(s1));
writeln('s1 : *', s1, '*');
writeln;

can't the apl_exec stdout also be put into the hs.thandlestream with the fpdup?


440bx

  • Hero Member
  • *****
  • Posts: 4070
Re: can someone straighten me out with this code?
« Reply #7 on: February 13, 2023, 12:40:32 am »
@Toby:

You've got over a hundred posts, by now you should have learned how to use code tags.  Already the moderators edited your first post to include code tags, that should have been enough for you to use them in future posts (it would be for most people.)

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Joanna

  • Hero Member
  • *****
  • Posts: 771
Re: can someone straighten me out with this code?
« Reply #8 on: February 13, 2023, 02:57:16 am »
Maybe he doesn’t know how to do code tags. It took me awhile to figure it out.
Toby just select pascal in the code combobox and these will appear in your edit window.
 “[ code=pascal][/code ]“

paste your code between the ][ parts

 ]paste your code here[
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

 

TinyPortal © 2005-2018