Discussion:
TFileStream.Read - Simple Question
secantau
2002-06-06 02:52:31 UTC
Permalink
I'm having trouble reading from a FileStream. I've searched the
delphi help and the list archive and can't seem to find much help.

I want to read the first 15 characters from a text file but I keep
getting a blank string (which it shouldn't be).

What's wrong with this code ?

var
MyFile : TFileStream;
sMsg: String;
PtrData : Pchar;

begin

If FileExists('C:\Mitmr.txt') then
begin

MyFile := TFileStream.Create('C:\Mitmr.txt',fmOpenRead or
fmShareDenyWrite);
MyFile.Seek(0, soFromBeginning);
MyFile.Read(PtrData^,15);
sMsg := string(PtrData);
showMessage(sMsg);
end;
end;
Michael Grimard
2002-06-06 21:09:33 UTC
Permalink
You don't need PtrData to achieve what you want, only set the length of
sMsg to 15.

This is what I use:

function TmxcUtil.ReadStrFromStream(Stream: Tstream, ASize: Integer):
string;
var
AStrValue : string;
begin
SetString(AStrValue, nil, ASize);
Stream.Read(Pointer(AStrValue)^, ASize);
Result := AStrValue;
end;

Michael

-----Original Message-----
From: secantau [mailto:***@primus.com.au]
Sent: Wednesday, June 05, 2002 10:53 PM
To: delphi-***@yahoogroups.com
Subject: [Delphi] TFileStream.Read - Simple Question



I'm having trouble reading from a FileStream. I've searched the
delphi help and the list archive and can't seem to find much help.

I want to read the first 15 characters from a text file but I keep
getting a blank string (which it shouldn't be).

What's wrong with this code ?

var
MyFile : TFileStream;
sMsg: String;
PtrData : Pchar;

begin

If FileExists('C:\Mitmr.txt') then
begin

MyFile := TFileStream.Create('C:\Mitmr.txt',fmOpenRead or
fmShareDenyWrite);
MyFile.Seek(0, soFromBeginning);
MyFile.Read(PtrData^,15);
sMsg := string(PtrData);
showMessage(sMsg);
end;
end;




The Delphi Top 100 Web Sites!
http://www.sandbrooksoftware.com/TS/TS2/delphi.shtml
---------------------------------------------------------------
Unsubscribe:delphi-programming-***@yahoogroups.com
List owner:delphi-programming-***@yahoogroups.com
---------------------------------------------------------------

Your use of Yahoo! Groups is subject to
http://docs.yahoo.com/info/terms/
Stephen Wood
2002-06-06 21:53:26 UTC
Permalink
or...

Stream.Read(AStrValue[1], ASize);

if you are put off by the pointer and dereferencing....

----- Original Message -----
From: "Michael Grimard" <***@maxt.com>
To: <delphi-***@yahoogroups.com>
Sent: Thursday, June 06, 2002 11:09 PM
Subject: RE: [Delphi] TFileStream.Read - Simple Question


You don't need PtrData to achieve what you want, only set the length of
sMsg to 15.

This is what I use:

function TmxcUtil.ReadStrFromStream(Stream: Tstream, ASize: Integer):
string;
var
AStrValue : string;
begin
SetString(AStrValue, nil, ASize);
Stream.Read(Pointer(AStrValue)^, ASize);
Result := AStrValue;
end;

Michael

-----Original Message-----
From: secantau [mailto:***@primus.com.au]
Sent: Wednesday, June 05, 2002 10:53 PM
To: delphi-***@yahoogroups.com
Subject: [Delphi] TFileStream.Read - Simple Question



I'm having trouble reading from a FileStream. I've searched the
delphi help and the list archive and can't seem to find much help.

I want to read the first 15 characters from a text file but I keep
getting a blank string (which it shouldn't be).

What's wrong with this code ?

var
MyFile : TFileStream;
sMsg: String;
PtrData : Pchar;

begin

If FileExists('C:\Mitmr.txt') then
begin

MyFile := TFileStream.Create('C:\Mitmr.txt',fmOpenRead or
fmShareDenyWrite);
MyFile.Seek(0, soFromBeginning);
MyFile.Read(PtrData^,15);
sMsg := string(PtrData);
showMessage(sMsg);
end;
end;




The Delphi Top 100 Web Sites!
http://www.sandbrooksoftware.com/TS/TS2/delphi.shtml
---------------------------------------------------------------
Unsubscribe:delphi-programming-***@yahoogroups.com
List owner:delphi-programming-***@yahoogroups.com
---------------------------------------------------------------

Your use of Yahoo! Groups is subject to
http://docs.yahoo.com/info/terms/






The Delphi Top 100 Web Sites!
http://www.sandbrooksoftware.com/TS/TS2/delphi.shtml
---------------------------------------------------------------
Unsubscribe:delphi-programming-***@yahoogroups.com
List owner:delphi-programming-***@yahoogroups.com
---------------------------------------------------------------

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Stephen Wood
2002-06-06 21:50:59 UTC
Permalink
Hi ,

Michael has pointed you in the right direction, but you also have a fundamental mistake in your code...a PChar is only a pointer, and you are trying to read the string into where the pointer is pointing to, but you have not allocated anything at all...so you are actually trying to load the first 15 characters into who-knows what address....

Another approach if you only want the first 15 chars is the following...

var
MyFile : TFileStream;
sData: array [0..14] of char;
begin

If FileExists('C:\Mitmr.txt') then
begin

MyFile := TFileStream.Create('C:\Mitmr.txt',fmOpenRead or fmShareDenyWrite);
MyFile.Seek(0, soFromBeginning);
MyFile.Read(sData,15);
showMessage(sMsg);
end;
end;

The will put your characters into a character array which is exactly like a PChar if you use @sData....

----- Original Message -----
From: "secantau" <***@primus.com.au>
To: <delphi-***@yahoogroups.com>
Sent: Thursday, June 06, 2002 4:52 AM
Subject: [Delphi] TFileStream.Read - Simple Question
Post by secantau
I'm having trouble reading from a FileStream. I've searched the
delphi help and the list archive and can't seem to find much help.
I want to read the first 15 characters from a text file but I keep
getting a blank string (which it shouldn't be).
What's wrong with this code ?
var
MyFile : TFileStream;
sMsg: String;
PtrData : Pchar;
begin
If FileExists('C:\Mitmr.txt') then
begin
MyFile := TFileStream.Create('C:\Mitmr.txt',fmOpenRead or
fmShareDenyWrite);
MyFile.Seek(0, soFromBeginning);
MyFile.Read(PtrData^,15);
sMsg := string(PtrData);
showMessage(sMsg);
end;
end;
The Delphi Top 100 Web Sites!
http://www.sandbrooksoftware.com/TS/TS2/delphi.shtml
---------------------------------------------------------------
---------------------------------------------------------------
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Loading...