_chmod, _wchmod 更改文件访问权限

 

_chmod, _wchmod

Change the file-permission settings.

int _chmod( const char *filename, int pmode );

int _wchmod( const wchar_t *filename, int pmode );

RoutineRequired HeaderOptional HeadersCompatibility
_chmod<io.h> <sys/types.h>, <sys/stat.h>, <errno.h>Win 95, Win NT
_wchmod<io.h> or <wchar.h><sys/types.h>, <sys/stat.h>, <errno.h>Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIBSingle thread static library, retail version
LIBCMT.LIBMultithread static library, retail version
MSVCRT.LIBImport library for MSVCRT.DLL, retail version

Return Value

Each of these functions returns 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.

Parameters

filename

Name of existing file

pmode

Permission setting for file

Remarks

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:

_S_IWRITE

Writing permitted

_S_IREAD

Reading permitted

_S_IREAD | _S_IWRITE

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

Example

/* 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>

void main( void )
{
   /* Make file read-only: */
   if( _chmod( "CHMOD.C", _S_IREAD ) == -1 )
      perror( "File not found/n" );
   else
      printf( "Mode changed to read-only/n" );
   system( "echo /* End of file */ >> CHMOD.C" );

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

Output

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

File Handling Routines

See Also   _access, _creat, _fstat, _open, _stat

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值