_chmod, _wchmod
Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

_chmod, _wchmod

Change the file-permission settings.

int _chmod( 
   const char *filename,
   int pmode 
int _wchmod( 
   const wchar_t *filename,
   int pmode 


Name of existing file.
Permission setting for file.

Return Value

These functions return 0 if the permission setting is successfully changed. A return value of –1 indicates that the specified file could not be found, in which case errno is set to ENOENT.


The _chmod function changes the permission setting of the file specified by filename. The permission setting controls read and write access to the file. The integer expression pmode contains one or both of the following manifest constants, defined in SYS\STAT.H:

Writing permitted.
Reading permitted.
Reading and writing permitted.

Any other values for pmode are ignored. When both constants are given, they are joined with the bitwise OR operator ( | ). If write permission is not given, the file is read-only. Note that all files are always readable; it is not possible to give write-only permission. Thus the modes _S_IWRITE and _S_IREAD | _S_IWRITE are equivalent.

_wchmod is a wide-character version of _chmod; the filename argument to _wchmod is a wide-character string. _wchmod and _chmod behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_tchmod _chmod _chmod _wchmod


Routine Required header Optional headers Compatibility
_chmod <io.h> <sys/types.h>, <sys/stat.h>, <errno.h> Win 98, Win Me, Win NT, Win 2000, Win XP
_wchmod <io.h> or <wchar.h> <sys/types.h>, <sys/stat.h>, <errno.h> Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


// crt_chmod.c
/* This program uses _chmod to
 * change the mode of a file to read-only.
 * It then attempts to modify the file.

#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>

int main( void )
   /* Make file read-only: */
   if( _chmod( "CRT_CHMOD.TXT", _S_IREAD ) == -1 )
      perror( "File not found\n" );
      printf( "Mode changed to read-only\n" );
   fflush( stdout );
   system( "echo /* End of file */ >> CRT_CHMOD.TXT" );

   /* Change back to read/write: */
   if( _chmod( "CRT_CHMOD.TXT", _S_IWRITE ) == -1 )
      perror( "File not found\n" );
      printf( "Mode changed to read/write\n" );
   fflush( stdout );
   system( "echo /* End of file */ >> CRT_CHMOD.TXT" ); 
}/* End of file */ 

Input: crt_chmod.txt

A line of text.


Mode changed to read-only
Access is denied.
Mode changed to read/write

See Also

File Handling Routines | _access | _creat | _fstat | _open | _stat | Run-Time Routines and .NET Framework Equivalents

© 2015 Microsoft