Mega Search
23.2 Million


Sign Up

Make a donation  
Only reads first binary written and not the others  
News Group: embarcadero.public.delphi.nativeapi

Hi

As it is written... when I try to readbuffer my file... it doesn't want to read else than what was written the first time

Code :

I have my VarString
I pass it through a encryption function
I take the Tbytes result and writeBuffer it in a file

when I read it, it goes well!

After, I restart the program... I append the same Tbytes to the file
I go and check... there is twice the same symbols

When I try to readBuffer all of it... it doesn't work...
I can only read      SetLength(Buffer, 25);    (who is the lenght of my VarString)
and not       SetLength(Buffer, 50);        (who is the lenght of twice the sentence)

???   :)

Vote for best question.
Score: 0  # Vote:  0
Date Posted: 7-Jan-2015, at 8:06 PM EST
From: Pierre Zarzour
 
Re: Only reads first binary written and not the others [Edit  
News Group: embarcadero.public.delphi.nativeapi
Thanks

The successfull other answer is making this post ok

I will explain if people read this post in time...

I was writing encrypted data in the file and to be able to read the rest, I had to read it block by block...

Instead of trying to read from 15 to 50.. I had to read from 25 to 50

Block by block

Nice!

:)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 8-Jan-2015, at 5:54 PM EST
From: Pierre Zarzour
 
Re: Only reads first binary written and not the others [Edit  
News Group: embarcadero.public.delphi.nativeapi
Pierre wrote:

> My goal is, in further times, use some data, encrypt it, put it in a
> database file and when I want to read it read from wich index I
> want to and decrypt what I will read

A database is a very different beast than a flat file.  For instance, each 
encrypted block of data you post to the database would be in its own independant 
record.  Then you would simply do an SQL query to search for the records 
you want to decrypt.

> For now... I click button1    5 times to put more than 1 time the data
> and try to read it from different indexes

I strongly suggest you use the framing technique I described earlier, eg:

{code}
var
  stream: TFilestream
  bytes: TBytes;
  len: Integer;
begin
  bytes := EncryptStr('I am encoding this text in a function', b);
  len := Length(bytes);

  stream := TFileStream.Create('fileNameAndDirectory', fmOpenReadWrite or 
fmShareDenyWrite);
  try
    stream.Seek(0, soEnd);
    stream.WriteBuffer(len, SizeOf(len));
    if len > 0 then
      stream.WriteBuffer(bytes[0], len);
  finally
    stream.Free;
  end;
end;
{code}

{code}
var
  stream: TFileStream;
  bytes: TBytes;
  len: Integer;
  index: Integer;
begin
  index := ...;
  bytes := nil;

  stream := TFileStream.Create('fileNameAndDirectory', fmOpenRead or fmShareDenyWrite);
  try
    while index > 0 do
    begin
      stream.ReadBuffer(len, SizeOf(len));
      if len > 0 then
        stream.Seek(len, soCurrent);
      Dec(index);
    end;
    Stream.ReadBuffer(len, SizeOf(len));
    if len > 0 then
    begin
      SetLength(bytes, len);
      Stream.ReadBuffer(bytes[0], len);
    end;
  finally
    stream.Free;
  end;

  // use bytes as needed...
  Label1.Text:=DecryptStr(bytes, ...);
end;
{code}

> I am doing this because if this doesn't work... imagine for a whole
> database

It would have been better to start with a database then.  Then you would 
have been coding with the assumption that you are dealing with separate records, 
accessed by indexes (or other criteria).  At the very least, you could define 
an interface that abstracts the data storage from your code so it does not 
know where the data is actually stored.  You could pass it a block of data 
and it will write it, and then later pass it an index and it would return 
the correct data.  You could then create an implementation that writes to 
your file, seeking and reading as needed (using the framing technique I mentioned 
earlier), and then later when you are ready you can switch to an implementation 
that accesses a database instead, and your reading/writing code would not 
know the difference.

--
Remy Lebeau (TeamB)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 8-Jan-2015, at 4:58 PM EST
From: Remy Lebeau (TeamB)
 
Re: Only reads first binary written and not the others [Edit  
News Group: embarcadero.public.delphi.nativeapi
My goal is, in further times, use some data, encrypt it, put it in a database file 
and when I want to read it
read from wich index I want to and decrypt what I will read

For now... I click button1    5 times to put more than 1 time the data and try to read it from different indexes

I am doing this because if this doesn't work... imagine for a whole database

It is a small test to prepare for the big thing
....

When you say a 50 bytes file should be read if you ask to read 50 bytes, I totally agree with you... 

That is all the matter of my questioning


Could you tell me how to attach a project please?

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 8-Jan-2015, at 4:34 PM EST
From: Pierre Zarzour
 
Re: Only reads first binary written and not the others [Edit  
News Group: embarcadero.public.delphi.nativeapi
Pierre wrote:

> a:=  EncryptStr('I am encoding this text in a function', b);
> try
> stream.Seek(0, soEnd);
> stream.WriteBuffer(a[0], length(a));

You are appending to the end of the file.  Existing data is preserved.

> SetLength(a, 25);
> try
> Stream.ReadBuffer(a[0], Length(a));

You are reading 25 bytes specifically.  If you have appended to the file 
multiple times, you are only reading the first set of data, not any data 
that was later appended.  If your data blocks are always 25 bytes in length 
(and why would they be?), you would have to seek to read subsequent blocks. 
 Or, if you are trying to decrypt the entire file, you have to take the file 
size in account when reading.

But, since you are appending different sets of data to the existing file, 
you probably should be framing your blocks inside the file so you know how 
large each block actually is.  Then you can read the frame header to know 
how large the frame body is.  Otherwise, what is the point of appending multiple 
sets of encrypted data to a file at all?  They would just blend together 
without any way to separate them during decryption.

What is your actual goal in all of this?  What are you trying to accomplish 
exactly?

> So as I said... imagine that I run two times the writing part... there
> is twice encryption in the file (i checked it and it works)

Yes, but WHY are you appending them to the same file?  And are they ALWAYS 
the same size after being encrypted (is the size of the input data being 
encrypted always the same?).  And when decrypting, do you need to decrypt 
them individually, or together as a whole?  It makes a BIG difference in 
how you design the layout of the file and how you read/write the file content.

> Then imagine that in the reading part... instead of _25_ I write 50...
> it doesn't work... it won't go further then the first "sentence" written 
in the file

Please show that code.  If there are 50 bytes in the file, and you tell ReadBuffer() 
to read 50 bytes, it will read 50 bytes (assuming EOF is not being reached). 
 If you are getting a failure, please show it.  There is nearly impossible 
for a 50-byte file to only return 25 bytes when asked to read 50 bytes.

> even thow I write 5 times with the writing part and there is 5 times
> the code in the file, it won't go more than the first sentence written

Then you are doing something wrong in your code.  Please create a small, 
complete, self-contained demo project that reproduces the same problem, and 
post the projct to the attachments forum for review.

--
Remy Lebeau (TeamB)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 8-Jan-2015, at 2:55 PM EST
From: Remy Lebeau (TeamB)
 
Re: Only reads first binary written and not the others [Edit  
News Group: embarcadero.public.delphi.nativeapi
Good morning

I know it sounds special but this is what it does... so... I am going to give you both codes... writing and reading :

{code}
var
stream= TFilestream
a= Tbytes

begin
stream := TFileStream.Create('fileNameAndDirectory', fmOpenReadWrite);


a:=  EncryptStr('I am encoding this text in a function', b);
             
try
stream.Seek(0, soEnd);
stream.WriteBuffer(a[0], length(a));

finally
stream.Free;
{code}


_Now for the reading_


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

SetLength(a, 25);

try
Stream.ReadBuffer(a[0], Length(a));
Label1.Text:=DecryptStr(a, b);

finally
  stream.Free;
{code}

So as I said... imagine that I run two times the writing part... there is twice encryption in the file (i checked it and it works)

Then imagine that in the reading part... instead of _25_ I write 50... it doesn't work... it won't go further then the first "sentence" written in the file

even thow I write 5 times with the writing part and there is 5 times the code in the file, it won't go more than the first sentence written

Hope you find out the problem?

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 8-Jan-2015, at 5:44 AM EST
From: Pierre Zarzour
 
Re: Only reads first binary written and not the others  
News Group: embarcadero.public.delphi.nativeapi
Pierre wrote:

> As it is written... when I try to readbuffer my file... it doesn't
> want to read else than what was written the first time

That implies that you are not overwriting the data that was written the first 
time.

> After, I restart the program... I append the same Tbytes to the
> file I go and check... there is twice the same symbols

That implies that you are appending the new bytes to the end of the existing 
file instead of overwriting it.

So you are contradicting your self here.  What is actually happening?

> When I try to readBuffer all of it... it doesn't work...

Define "doesn't work".  If there are 50 bytes in the file, you should be 
able to read all 50 bytes.  If ReadBuffer() can only read 25 bytes, than 
either there are only 25 bytes in the file to begin with, you are are starting 
your reading at the 25th byte of the file instead of the 0th byte.

> I can only read      SetLength(Buffer, 25);    (who is the lenght of my 
VarString)
> and not       SetLength(Buffer, 50);        (who is the lenght of twice 
the sentence)

Please show your actual code, both writing and reading.  You are clearly 
doing something wrong.

--
Remy Lebeau (TeamB)

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