Mega Search
23.2 Million


Sign Up

Make a donation  
Stream.ReadBuffer  
News Group: embarcadero.public.delphi.nativeapi

Hi

1.
I would like to know how to read from a file

I've got the writebuffer code but it seams not to work exactly the same for reading

Could you help me?

Thanks



2.
And... I would like to put what will be read in a variable so i can decrypted it after
because I encrypted it before putting it in the binary file

for example : 

var
aa: Tbytes

aa:= readbuffer ??

and then I take  aa   and pass it to my decryption function

Thanks

Vote for best question.
Score: 0  # Vote:  0
Date Posted: 7-Jan-2015, at 6:01 PM EST
From: Pierre Zarzour
 
Re: Stream.ReadBuffer [Edit] [Edit]  
News Group: embarcadero.public.delphi.nativeapi
Yes!


It works fine...


All I had to do was to seek from the beginning of the second sentence written

For what I will do there is no problem since every line will have 75 characters

So I will read blocs of 75 characters

This ends a super chapter lol

Thanks

Hope to have tips from you again!

Pierre

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 8-Jan-2015, at 5:31 PM EST
From: Pierre Zarzour
 
Re: Stream.ReadBuffer [Edit] [Edit]  
News Group: embarcadero.public.delphi.nativeapi
Pierre wrote:

> You see the      SetLength(a, 15);    with the
> Stream.Seek(0, soBeginning);
> It works well
> 
> When I put SetLength(a, 15); with
> Stream.Seek(5, soBeginning);
> it writes :
>
> no mapping exists for the unicode character in the multi-bytes target code 
page

That has NOTHING to do with file I/O.  That is an error you can get when 
converting string data from one encoding to another and the data is malformed. 
 My guess would be your decryption is failing when you start reading from 
5 bytes into the file (since the initial encryption wrote more than 5 bytes). 
 You are reading the encrypted data wrong, so you are passing malformed data 
into your decryption routine, which is likely just spitting out garbage instead 
of raising a decrypt error, and then you are trying to convert that garbage 
to a String, which is likely to throw the codepage error.

> and when I start from 0 it works...

Right, because you are starting the reading from the beginning of the encrypted 
data, so you read the encrypted block correctly, then decrypt it correctly, 
then convert it to a String correctly.

LOCATION LOCATION LOCATION.

> If he finds characters from 0 to 25 it should find it from 5 to 20

Yes, but that does not meant that bytes 5-20 have the same meaning to code 
that processes them as bytes 0-25 would have.

--
Remy Lebeau (TeamB)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 8-Jan-2015, at 5:07 PM EST
From: Remy Lebeau (TeamB)
 
Re: Stream.ReadBuffer [Edit] [Edit]  
News Group: embarcadero.public.delphi.nativeapi
That is a good interogation!?

But my code works for encryption and decrytpion...

It stops working when I seek from 5

{code}
stream := TFileStream.Create('c:\data.cus', fmOpenReadWrite);

SetLength(a, 15);
Stream.Seek(0, soBeginning);
try
Stream.ReadBuffer(a[0], Length(a));
Label1.Text:=DecryptStr(a, b);
finally
stream.Free;
end;
{code}

You see the      SetLength(a, 15);    with the
Stream.Seek(0, soBeginning);

It works well

When I put SetLength(a, 15); with
Stream.Seek(5, soBeginning);

it writes :

no mapping exists for the unicode character in the multi-bytes target code page

and when I start from 0 it works...

If he finds characters from 0 to 25 it should find it from 5 to 20

:)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 8-Jan-2015, at 4:16 PM EST
From: Pierre Zarzour
 
Re: Stream.ReadBuffer [Edit]  
News Group: embarcadero.public.delphi.nativeapi
Pierre wrote:

> _I would like to have :_


And your question is...?

> The error is :
> 
> _No mapping in the target page_   // in french with more words than that 
but... this is the error

That error has nothing to do with file I/O, and that is not an error message 
that any file I/O routine would throw.  So where is it actually coming from? 
 Your decryption code?

--
Remy Lebeau (TeamB)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 8-Jan-2015, at 1:42 PM EST
From: Remy Lebeau (TeamB)
 
Re: Stream.ReadBuffer [Edit]  
News Group: embarcadero.public.delphi.nativeapi
Hi

Thanks for your help

Yes I read the documentation about Tstream

It doesn't talk about the lenght, the seek, the () and what to put in it...  

Maybe you have a place that says it... :)

_I would like to have :_


{code}
Var
varStream: TFileStream

varStream:= TFileStream.Create('FileNameAndDirectory', fmOpenReadWrite);


SetLength(with possiblilities that goes in here);
Stream.Seek(with possiblilities that goes in here);

other functions

try
Stream.ReadBuffer(with what goes in here);
Label1.Text:= theVarThatCanBeRead

finally
  stream.Free;
{code}

hope this can help  ;)


_Now for my second part of the answer_  :)

I used both
Stream.Seek(5, soBeginning);
or: 
Stream.Position := 5;

{code}
stream := TFileStream.Create('fileNameAndDirectory', fmOpenReadWrite);

SetLength(VarTbytes, 5);
Stream.Seek(5, soBeginning);

try
Stream.ReadBuffer(VarTbytes[0], 5);    or   Stream.ReadBuffer(VarTbytes[0], Length(VarTbytes));
Label1.Text:=DecryptStr(VarTbytes, b);          //this is uncrypting what is read

Finally
stream.Free;
{code}

The error is :

_No mapping in the target page_   // in french with more words than that but... this is the error


And I choosed from index 5 during 5 indexes so I won't read more than was is written

Thanks for your help and if you have ideas for detailed documentation on delphi...
feel free  :)

Pierre :)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 8-Jan-2015, at 6:14 AM EST
From: Pierre Zarzour
 
Re: Stream.ReadBuffer  
News Group: embarcadero.public.delphi.nativeapi
Pierre wrote:

> Now... I am trying to read from a certain point in the file
> 
> I can read 5 letters or 12 letters instead of everything but I
> would like to start for example at index 5 in the file...

You need to Seek() the stream to the desired Position before then calling 
ReadBuffer():

{code}
var
  aa: TBytes;
begin
  ...
  Stream.Seek(5, soBeginning);
  // or: Stream.Position := 5;
  SetLength(aa, NumberOfBytesToRead);
  Stream.ReadBuffer(aa[0], Length(aa));
  ...
end;
{code}

Have you read the documentation for TStream yet?

> so I changed the 0 for 5 but it doesn't work  :

Of course not.  The Stream is still at Position 0, all you did was tell ReadBuffer() 
to read the file data into the TBytes memory starting at the 5th array element, 
which will of course fail because you are reading more data than the TBytes 
has been allocated to hold.

--
Remy Lebeau (TeamB)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 7-Jan-2015, at 9:17 PM EST
From: Remy Lebeau (TeamB)
 
Re: Stream.ReadBuffer  
News Group: embarcadero.public.delphi.nativeapi
It works perfectly and my Tbytes is being decrypted without problem afterwords!!

:)

Now... I am trying to read from a certain point in the file

I can read 5 letters or 12 letters instead of everything but I would like to start for example at index 5 in the file... 

so I changed the 0 for 5 but it doesn't work  :

Stream.ReadBuffer(aa[5], Length(aa));

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 7-Jan-2015, at 7:13 PM EST
From: Pierre Zarzour
 
Re: Stream.ReadBuffer  
News Group: embarcadero.public.delphi.nativeapi
Pierre wrote:

> I've got the writebuffer code but it seams not to work exactly
> the same for reading

You have to allocate memory for the array before you can then read data into 
it, eg:

{code}
var
  aa: TBytes;
begin
  ...
  SetLength(aa, NumberOfBytesToRead);
  Stream.ReadBuffer(aa[0], Length(aa));
  ...
end;
{code}

As such, you have to know ahead of time how many bytes you want to read. 
 If you are trying to read the entire file, you would use the file size (though 
that is not always the best option).  Otherwise, you have to read the file 
in fixed-length blocks if you know the size of them, otherwise you have to 
store the actual block sizes in the file itself and read them before then 
reading the blocks.

--
Remy Lebeau (TeamB)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 7-Jan-2015, at 6:24 PM EST
From: Remy Lebeau (TeamB)