Re: fclose() ??

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"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.


.



Relevant Pages

  • Re: Browser cache security
    ... > Is there any way to stop writing any files on disc cache ... when I browsing web ... Google ramdrive freeware "windows xp" ...
    (comp.security.misc)
  • Re: ferror()
    ... pay attention to the return value of fclose() ... >>particularly if the file is opened for writing. ... Stephen Howe ...
    (comp.lang.c)
  • Re: cant open file for writing
    ... sueiz su wrote: ... >> <SNIP cannot open files ad lib... ... > 1)I do use fclose after writing; ...
    (comp.soft-sys.matlab)
  • Re: cant open file for writing
    ... Jos wrote: ... >> 1)I do use fclose after writing; ... of course,I use fclose after every opening and reading,but the ...
    (comp.soft-sys.matlab)
  • Browser cache security
    ... Is there any way to stop writing any files on disc cache when I browsing web ... Regards, ...
    (comp.security.misc)