Lazarus

Programming => General => Topic started by: cdbc on August 18, 2022, 01:25:35 pm

Title: Strange, i cannot write to a stream, what gives?
Post by: cdbc on August 18, 2022, 01:25:35 pm
Hi
I've got a very strange problem, a stream won't let me write to it?!?

I need some advice please.
Attached is a test project, to show what i mean...
Regards Benny
Title: Re: Strange, i cannot write to a stream, what gives?
Post by: PascalDragon on August 18, 2022, 01:39:06 pm
It seems that the compiler picks the wrong overload of TStream.Read. You can work around this by using CopyFrom instead:

Code: Pascal  [Select][+][-]
  1. procedure TfrmConcStreams.ConcatenateStreams(aStream1: TMemoryStream;aStream2: TMemoryStream);
  2. const CrLf = #13#10'-----'#13#10;
  3. var
  4.   Buffer: TBytes;
  5.   Res: int64;
  6. begin
  7.   aStream2.Position := 0;
  8.   aStream1.Seek(0, soEnd);
  9.   aStream1.Write(CrLf, 9);
  10.   aStream1.CopyFrom(aStream2, aStream2.Size);
  11.   aStream1.Position := 0;
  12.   aStream2.Position := 0;  
  13. end;

Sidenote: you can use Length(CrLf) instead of hardcoding a 9 as well.

Edit: it seems that this is fixed in 3.3.1, maybe I can find the corresponding commit and merge it back to 3.2.3.
Title: Re: Strange, i cannot write to a stream, what gives?
Post by: ASerge on August 18, 2022, 08:32:08 pm
It seems that the compiler picks the wrong overload of TStream.Read. You can work around this by using CopyFrom instead:
If FPC 3.2.2 TStream does not have overloaded Read, Write functions, in which the parameter has the type TBytes.
Therefore, need to use Read(buffer[0],...) instead of Read(buffer,...). The same with Write.
Title: Re: Strange, i cannot write to a stream, what gives?
Post by: PascalDragon on August 19, 2022, 09:05:56 am
It seems that the compiler picks the wrong overload of TStream.Read. You can work around this by using CopyFrom instead:
If FPC 3.2.2 TStream does not have overloaded Read, Write functions, in which the parameter has the type TBytes.
Therefore, need to use Read(buffer[0],...) instead of Read(buffer,...). The same with Write.

Ah, right! My IDE fooled me here, cause I have the sources set to ones from main despite using 3.2.2 as compiler. %)

@cdbc: do as ASerge said and then this will also work with newer versions of FPC. Or you can use my alternative with the CopyFrom (which will also work across versions).
TinyPortal © 2005-2018