Pregunta

I want to know what are the createfile access and sharing modes that match with the following fopen access modes :

  • "rb+c"
  • "wb+c"

I checked for a nice table with all this information but did not find it.

¿Fue útil?

Solución

The fopen() 'c' flag does not applicable to CreateFile(), in that it only applies a mode related to opening a file, not creating one.

For Linux, 'fopen()' the flags indicated in the question are as follows:

  • "rb+c"
    • r Open file for reading. The stream is positioned at the beginning of the file.
    • b Strictly for compatibility with C89 and has no effect; the 'b' is ignored on all POSIX conforming systems, including Linux.
    • + Extends the r flag (above) to also allow writing to the file.
    • c Do not make the open operation, or subsequent read and write operations, thread cancellation points.
  • "wb+c"
    • w Open file for writing. The file is created if it does not exist, otherwise the file is truncated to zero length. The stream is positioned at the beginning of the file.
    • b Strictly for compatibility with C89 and has no effect; the 'b' is ignored on all POSIX conforming systems, including Linux.
    • + Extends the w flag (above) to also allow reading of the file.
    • c Do not make the open operation, or subsequent read and write operations, thread cancellation points.

Translated to CreateFile():

  • "rb+c"
  • dwDesiredAccess GENERIC_READ|GENERIC_WRITE
    dwShareMode FILE_SHARE_READ|FILE_SHARE_WRITE
    dwCreationDisposition OPEN_EXISTING
    dwFlagsAdAttributes FILE_ATTRIBUTE_NORMAL

    CreateFile(
       ...,
       GENERIC_READ|GENERIC_WRITE, 
       FILE_SHARE_READ|FILE_SHARE_WRITE,
       NULL, 
       OPEN_EXISTING,
       FILE_ATTRIBUTE_NORMAL,
       ...
       );
    

  • "wb+c"
  • dwDesiredAccess GENERIC_READ|GENERIC_WRITE
    dwShareMode FILE_SHARE_READ|FILE_SHARE_WRITE
    dwCreationDisposition TRUNCATE_EXISTING|OPEN_ALWAYS
    dwFlagsAdAttributes FILE_ATTRIBUTE_NORMAL

    CreateFile(
       ...
       GENERIC_READ|GENERIC_WRITE, 
       FILE_SHARE_READ|FILE_SHARE_WRITE,
       NULL, 
       TRUNCATE_EXISTING|OPEN_ALWAYS,
       FILE_ATTRIBUTE_NORMAL,
       ...
       );
    

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top