>Read returning 0 has nothing (read should have nothing) to do with you
buffer memory allocation.
>It is more likely it has failed to open the file on disk, or has
actually reached the EOF.

Yes, "should have nothing" is correct.  But as my test program reads a
file which I know exists and is not empty (actually I often use
ParamStr(0), which should definitely fit that definition), and since
TFileStream.Create is successful, I can (read: should be able to) be
confident that I have a valid, open, file handle.  So I think that the
internal ReadFile call must be failing, for some other reason.  Of
course calling GetLastError immediately stops the problem from occurring
at all... :(

I could indeed do as you suggest below.  I'm just looking for an
explanation to restore my faith in casting strings to PChars.  And in
TFileStream.  The help tries to discourage you from using FileRead...

Cheers,

Carl Reynolds                      Ph: +64-9-4154790
CJN Technologies Ltd.             Fax: +64-9-4154791
[EMAIL PROTECTED]                DDI: +64-9-4154795
PO Box 302-278, North Harbour, Auckland, New Zealand
12 Piermark Drive, North Harbour Estate, Auckland, NZ
Visit our website at http://www.cjntech.co.nz/

> -----Original Message-----
> From: Myles Penlington [SMTP:[EMAIL PROTECTED]]
> Sent: Friday, August 06, 1999 2:12 PM
> To:   Multiple recipients of list delphi
> Subject:      RE: Strings, PChars, and TFileStream.Read
> 
> Read returning 0 has nothing (read should have nothing) to do with you
> buffer memory allocation.
> It is more likely it has failed to open the file on disk, or has
> actually reached the EOF.
> 
> However for these purposes I do it this way.
> 
> var
>   Buffer: array [0..MaxBuf] of Char;  {Compatible with PChar, MaxBuf
> being the maximum to read at any one time}
>  MyPChar: PChar;
> ...
> if  Read( Buffer, 1 ) = 0 then ...
> 
> or
>   MyPChar := @Buffer
> if Read( MyPChar^, 1) = 0 then ...
> 
> This has the advantage that the buffer is allocated on stack instead
> of on the Heap. 
> 
> Myles.
> 
> -----Original Message-----
> From: Carl Reynolds [SMTP:[EMAIL PROTECTED]]
> Sent: Friday, August 06, 1999 12:13 PM
> To:   Multiple recipients of list delphi
> Subject:      Strings, PChars, and TFileStream.Read
> 
> Hi all.  I thought it was safe to use PChar(MyString) in Delphi 4
> whenever you needed a PChar, but I'm having a problem with the
> following code (somewhat simplified):
> 
> var LChar: string;
> begin
>       with TFileStream.Create(ParamStr(0), // Any file will do - this
> is just one guaranteed to exist
>         fmOpenRead or fmShareDenyNone) do try
>               LChar := 'X';
>               if Read(PChar(LChar)^, 1) = 0 then begin
>                       ShowMessage('Say what?!!');
>               end
>               else ShowMessage('Whew!');
>       finally
>               Free;
>       end;
> end;
> 
> In some cases, depending on what other code I've got in unrelated
> places, I get 0 returned - ie., the TFileStream thinks that it can't
> read from the file or that the file is empty, which obviously should
> never be the case.  Things are complicated by the fact that the error
> rarely happens, and when it does putting in a ShowMessage or a
> breakpoint cause it to _not_ happen!
> 
> So what I'm really asking is, is the code safe?  Should I do one of
> the following, which all seem to work (but do they really - for all
> cases?):
> 
> 1/.
> ...
>               LChar := 'X';
>               UniqueString(LChar); // Is this really necessary?
> ...
> 
> 2/.
> ...
>               SetLength(LChar, 1); // Should I be more explicit about
> memory allocation?
> ...
> 
> 3/.
> ...
> var LChar: string;
>     LPChar: PChar;
> <snip>
>               LChar := 'X';
>               LPChar := PChar(LChar); // Or is this a safer way to do
> it?
>               if Read(LPChar)^, 1) = 0 then begin
> ...
> 
> Or can't I mix PChars and strings at all?
> 
> ...
> var LPChar: PChar;
> <snip>
>               StrAlloc(LPChar, 1);
>               if Read(LPChar)^, 1) = 0 then begin
> ...
> 
> Explanations for any of this gratefully received.
> 
> Cheers,
> 
> Carl Reynolds                      Ph: +64-9-4154790
> CJN Technologies Ltd.             Fax: +64-9-4154791
> [EMAIL PROTECTED]                DDI: +64-9-4154795
> PO Box 302-278, North Harbour, Auckland, New Zealand
> 12 Piermark Drive, North Harbour Estate, Auckland, NZ
> Visit our website at http://www.cjntech.co.nz/

application/ms-tnef

Reply via email to