Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

fmShareDenyWrite/None - cannot open file for reading in Notepad

312 views
Skip to first unread message

Kristofer Skaug

unread,
Nov 29, 2001, 4:43:28 AM11/29/01
to
I'm creating a file using TFileStream.Create(..., fmCreate or
fmShareDenyWrite);
The file stores text data which I'd like to be able to inspect whlie the
file is still open for writing.
I was hoping to be able to open the file using NotePad for for example,
but NotePad cannot open this file while it is being written to, not even
just for reading.
After opening the .txt file with Notepad (simply doubleclicking in
Explorer), I get the message from Notepad: "Cannot open the <path> file.
Make sure a disk is in the drive you specified".

Changing the share option to fmShareDenyNone doesn't help! Very odd.

Is this 'as it should be' ("feature" of Notepad) or is there an error in
how Delphi handles fmShareDenyWrite?
BTW I'm using D6.1 Pro on WinNT4.0 SP6.
Any ideas?

Kristofer


Kristofer Skaug

unread,
Nov 29, 2001, 4:56:05 AM11/29/01
to
"Kristofer Skaug" <k.s...@satserv.nl> wrote in message
news:3c060320_1@dnews...

>
> Changing the share option to fmShareDenyNone doesn't help! Very odd.

worse yet, trying to read the file from another Delphi app, opening the
file using fmOpenRead also fails.
(now I'm *really* confused...)


Eric Jansen

unread,
Nov 29, 2001, 6:05:45 AM11/29/01
to

"Kristofer Skaug" <k.s...@satserv.nl> wrote in message
news:3c060320_1@dnews...
> I'm creating a file using TFileStream.Create(..., fmCreate or
> fmShareDenyWrite);

The VCL ignores the sharing flags if the open mode is fmCreate.
I am not sure if the Windows CreateFile API does support creating a new file
with sharing - if so, it could be considered a VCL bug (unless it is
documented somewhere, of course).

> The file stores text data which I'd like to be able to inspect whlie the
> file is still open for writing.
> I was hoping to be able to open the file using NotePad for for example,
> but NotePad cannot open this file while it is being written to, not even
> just for reading.

Notepad just opens the file exclusively, which is the case for most simple
applications.

--
Eric Jansen

Find your answers at:
Newsgroups archives: http://groups.google.com
Torry's Delphi Pages: http://www.torry.net
Delphi Super Page: http://delphi.icm.edu.pl

Eric Jansen

unread,
Nov 29, 2001, 6:08:52 AM11/29/01
to
"Kristofer Skaug" <k.s...@satserv.nl> wrote in message
news:3c060615_2@dnews...

> Changing the share option to fmShareDenyNone doesn't help! Very odd.

The problem is the fmCreate. If the file does not exist, create it first and
then reopen the file with fmOpenWrite + fmDenyNone (or something like that).

Kristofer Skaug

unread,
Nov 29, 2001, 7:19:10 AM11/29/01
to
"Eric Jansen" <er...@do.not.spam.keyword.nl> wrote in message
news:3c061695$1_1@dnews...

>
> The VCL ignores the sharing flags if the open mode is fmCreate.
> I am not sure if the Windows CreateFile API does support creating a new
file
> with sharing - if so, it could be considered a VCL bug (unless it is
> documented somewhere, of course).

He, thanks a million! This 'feature' of the fmCreate option is not
documented anywhere that I can see.
I have seen C++ code which creates shareable files, so it must be
possible.
So it certainly looks like a VCL bug then.

> Notepad just opens the file exclusively, which is the case for most
simple
> applications.

AFAICS - it is able to open log files as per above, which are being
written to by another application.

Kristofer


Eric Jansen

unread,
Nov 29, 2001, 7:38:54 AM11/29/01
to
"Kristofer Skaug" <k.s...@satserv.nl> wrote in message
news:3c06279e_1@dnews...

> I have seen C++ code which creates shareable files, so it must be
> possible.

I just tried it with CreateFile API, and it is possible.

f := CreateFile('c:\test.txt', GENERIC_WRITE, FILE_SHARE_READ or
FILE_SHARE_WRITE,
nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);

>> Notepad just opens the file exclusively, which is the case for most
simple
>> applications.
> AFAICS - it is able to open log files as per above, which are being
> written to by another application.

You are right.

Kristofer Skaug

unread,
Nov 29, 2001, 7:57:31 AM11/29/01
to
"Eric Jansen" <er...@do.not.spam.keyword.nl> wrote in message
news:3c062c6a$1_1@dnews...

>
> f := CreateFile('c:\test.txt', GENERIC_WRITE, FILE_SHARE_READ or
> FILE_SHARE_WRITE,
> nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);

Thanks. But, in the interest of having Kylix-portable code (i.e. sticking
with TFileStream), I will go with your original suggestion (i.e. to
'fmCreate' the file first, then close and re-open with fmOpenReadWrite.
I've implemented that now and it works fine!

Kristofer

Damien Honeyford

unread,
Nov 29, 2001, 2:33:44 PM11/29/01
to
Hi,

"Eric Jansen" <er...@do.not.spam.keyword.nl> wrote in message

news:3c061695$1_1@dnews...

> The VCL ignores the sharing flags if the open mode is fmCreate.

Not true; the sharing flags are indeed taken into account, as
TFileStream uses FileCreate if only fmCreate is passed as the Mode
parameter, otherwise it uses FileOpen with whatever Mode you passed.
This is from D4, so if things have changed in 5 or 6 then pardon me, but
I doubt they would ignore sharing modes.

Damien


Eric Jansen

unread,
Nov 29, 2001, 5:18:47 PM11/29/01
to
"Damien Honeyford" <neim...@hotmail.com> wrote in message
news:3c068bfb_1@dnews...

> > The VCL ignores the sharing flags if the open mode is fmCreate.
>
> Not true; the sharing flags are indeed taken into account, as
> TFileStream uses FileCreate if only fmCreate is passed as the Mode
> parameter, otherwise it uses FileOpen with whatever Mode you passed.
> This is from D4, so if things have changed in 5 or 6 then pardon me, but
> I doubt they would ignore sharing modes.

Yes, it *looks* like the source does it that way, but the 'problem' lies in
the fact that fmCreate is defined as $ffff, so or-ing it with any share mode
will have no effect.

Michael Winter

unread,
Nov 29, 2001, 5:37:29 PM11/29/01
to
Damien Honeyford schrieb:

> > The VCL ignores the sharing flags if the open mode is fmCreate.
>
> Not true; the sharing flags are indeed taken into account, as
> TFileStream uses FileCreate if only fmCreate is passed as the Mode
> parameter

And FileCreate doesn't care about the file mode:

function FileCreate(const FileName: string): Integer;
begin
Result := Integer(CreateFile(PChar(FileName),
GENERIC_READ or GENERIC_WRITE,
0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0));
end;

OTOH, with fmCreate you simply _can't_ combine any file modes:

fmCreate = $FFFF;
...
fmShareExclusive = $0010;
fmShareDenyWrite = $0020;
fmShareDenyRead = $0030;
fmShareDenyNone = $0040;

But ist should be easy to implement a TFileStream descendant with a
changed constructor only.

-Michael

0 new messages