Re: Questions of CreateFile and WriteFile

From: Arnaud Debaene (adebaene_at_club-internet.fr)
Date: 02/13/05


Date: Sun, 13 Feb 2005 23:24:48 +0100

ad wrote:
> 1. CreateFile(fileName, FLAG,
> FILE_SHARE_WRITE|FILE_SHARE_READ,
> NULL,
> OPEN_EXISTING,
> NULL,
> NULL);
>
> To share the access to "fileName" among processes, does FLAG have to
> be set to "GENERIC_WRITE|GENERIC_READ"?

The second parameter ("FLAG" as you have called it) is the desired access
mode to the file(read, write, execute, etc...). It has nothing to do with
the saring mode of the file, except that you can't open a file in a given
mode (say, for writing), if another process has already opened the file with
a sharing mode that restrict this access to other processes (ie, it has
opened the file without FILE_SHARE_WRITE).

> To share the access to "fileName" among threads in a process, does
> FLAG have to be set to "GENERIC_WRITE|GENERIC_READ" too?
File sharing and protection is at process level : Threads of the same
process have complete access to one another resources.

> 2. WriteFile
>
> "The WriteFile function writes data to a file at the position
> specified by the file pointer. This function is designed for both
> synchronous and asynchronous operation."
>
> How to specify the position in a file for writing in Win32?
SetFilePointer(Ex) or SetEndOfFile.

> Can "WriteFile" to write data beyond END-OF-FILE? (For example, file
> size is 1K, but WriteFile from the position of 20K.)
No. But you can use SetEndOfFile to fill from 1K to 20K offset with
undefined data. If you need to create huge files which contains a lot of
"empty" spaces (logically, 0s), you may be intersted in sparse files : see
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/sparse_files.asp

> MSDN says that "You cannot perform asynchronous write operations on
> ..., or disk files." Why can't write asynchronously in a disk file?
This is true only on Windows 95/98/Me, because those systems don't support
asynchronous file access!

Arnaud
MVP - VC



Relevant Pages

  • Re: CreateFile() and FILE_FLAG_WRITE_THROUGH
    ... BUT this will not work UNLESS you specify the ... FILE_FLAG_NO_BUFFERING flag, ... I have now proved to myself that file metadata is not written ... disk structure to be consistent, this incurs a cost and that's why the ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Letting a user select a color for a control
    ... CC_FULLOPEN flag instead of the CC_SOLIDCOLOR flag in order to do this. ... is there any way to specify where on the screen the Color Dialog ... > specify an initial color selection. ... >> something before calling aDialogColor. ...
    (microsoft.public.access.modulesdaovba)
  • Re: date from yesterday
    ... | | versions of date that uses the -v flag. ... | | adjust units, but without the -/+ preceding the unit it gives the ... Users in countries that didn't switch from the Julian ... Fully functional demos available by email request (specify OS). ...
    (comp.unix.sco.misc)
  • Re: Problem with fopen, linux, and RS-232, possibly
    ... Are those pins you specified the DCD signal? ... you may want the O_NOCTTY flag to be present. ... Try writing the program using openinstead, and specify the exact ... may be unaware of its "readiness", then any opencall may not block. ...
    (comp.lang.c)
  • Re: how to import two .tlb uses same interface?
    ... Unless you specify no_namespace flag on #import directive, ... interfaces will be in different namespaces. ...
    (microsoft.public.vc.atl)