Re: fclose() ??
- From: "Frank Hickman [MVP]" <fhickman_NOSP@xxxxxxxxxxxxxxx>
- Date: Thu, 14 Apr 2005 23:18:41 -0400
"Jacques Cooper" <jcooper@xxxxxxxxxxxxxx> wrote in message
news:ePyo1hWQFHA.3140@xxxxxxxxxxxxxxxxxxxxxxx
> Hello,
>
> I am using VC++ 6.0, and I am having problems reading and writing a file.
>
> I have two processes; A and B. Process A creates a file, and then closes
> the file. After Process A closes the file, Process B is spawned (from
> Process A).
> Process B tries to read the file, that was created from Process A.
> However, when Process B tries to read the file, Process B generates a
> "File sharing access violation" error.
>
> Does fclose() "close" the file?
>
> Process A:
> FILE* pFile=NULL;
>
> int nRetVal = 0;
>
> pFile = fopen( (LPCTSTR)strFilename, _T("a+") ) ;
>
> if ( !pFile )
> {
> AfxMessageBox( _T("Unable to open ") + strFilename, MB_OK |
> MB_ICONSTOP ) ;
> return;
> }
>
> // Write some stuff in the file.
>
> nRetVal = fclose( pFile ) ;
> if (nRetVal == 0)
> {
> // Create Process B;
> }
>
>
> Process B:
> CFile sourceFile;
>
> if (!sourceFile.Open(strFilename,CFile::modeRead |
> CFile::shareDenyWrite,
> &ex))
> {
> // File sharing access error:
> ex.GetErrorMessage(szError,1024);
> AfxMessageBox(szError);
> return;
> }
>
> TIA,
> Jacques
It is most likely because the disc cache has not been written to the disc
yet which keeps the file locked. Don't confuse this with the actual file
stream buffer which does get flushed when the file is closed. I seem to
recall that there is an API for flushing the disc cache but I cannot
remember what it was...in any event, you can specify "c" in the mode
parameter of the fopen to tell the OS to write the cache when fflush is
called and call it before calling fclose. fclose does do a flush but it may
give the system time to complete the write before the open in process B.
--
============
Frank Hickman
Microsoft MVP
NobleSoft, Inc.
============
Replace the _nosp@m_ with @ to reply.
.
- Follow-Ups:
- Re: fclose() ??
- From: Jacques Cooper
- Re: fclose() ??
- From: Alexander Grigoriev
- Re: fclose() ??
- References:
- fclose() ??
- From: Jacques Cooper
- fclose() ??
- Prev by Date: fclose() ??
- Next by Date: Re: menu problem on a dialog form
- Previous by thread: fclose() ??
- Next by thread: Re: fclose() ??
- Index(es):
Relevant Pages
|