Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Filestreamm read data from file


This question is answered.


Permlink Replies: 1 - Last Post: Dec 21, 2017 9:55 AM Last Post By: Remy Lebeau (Te...
madammar ellias

Posts: 111
Registered: 8/17/17
Filestreamm read data from file  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 21, 2017 9:44 AM
i am trying to write and read string from file that stored in android

i have written the file but when i try to read what inserted inside that file and use if condintion to compare the string it seams that text are not matched

and i have write only '1' as string to the file but whenever i use if str = '1' then add it doesnt meat the condition any dea why ? the result that comes out is already 1
procedure TForm3.btnClick(Sender: TObject);
var
sw: TStreamWriter;
begin
sw := TStreamWriter.Create('mst.txt', False, TEncoding.UTF8);
try
sw.WriteLine ('1');
finally
sw.Free;
end;
Memo1.Lines.Add ('File written');
end;
 
procedure TForm3.Timer1Timer(Sender: TObject);
var
fStr: TFileStream;
Str : string;
Bytes: TBytes;
begin
  fStr := TFileStream.Create('mst.txt', fmOpenRead);
try
if fStr.Size > 0 then begin
SetLength(Bytes, fStr.Size);
fStr.Read(Bytes[0], fStr.Size);
str := TEncoding.UTF8.GetString(Bytes);
end;
 
 
finally
fStr.Free;
end;
 
if sametext(str, '1') then
begin
Memo1.Lines.Add (str);
end;
 
end;
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Filestreamm read data from file
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 21, 2017 9:55 AM   in response to: madammar ellias in response to: madammar ellias
madammar ellias wrote:

i have written the file but when i try to read what inserted inside
that file and use if condintion to compare the string it seams that
text are not matched

Why are you reading the file using TFileStream directly, instead of
using TStreamReader to match your use of TStreamWriter?

You are not accounting for the fact that TStreamWriter will write a
UTF-8 BOM to your file, but your TFileStream code is not skippping that
BOM. That is why the string you read does not match the string you
wrote.

TStreamReader would take the BOM into account for you.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02