_chmod, _wchmod 更改文件访问权限

原创 2006年05月30日 18:17:00
 

_chmod, _wchmod

Change the file-permission settings.

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

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

Routine Required Header Optional Headers Compatibility
_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.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import 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

相关文章推荐

Linux chmod --改变文件/目录的访问权限

chmod命令用于改变linux系统文件或目录的访问权限。用它控制文件或目录的访问权限。该命令有两种用法。一种是包含字母和操作符表达式的文字设定法;另一种是包含数字的数字设定法。 Linux系统中的...

使用chmod和数字改变文件或目录的访问权限

文件和目录的权限表示,是用rwx这三个字符来代表所有者、用户组和其他用户的权限。有时候,字符似乎过于麻烦,因此还有另外一种方法是以数字来表示权限,而且仅需三个数字。 r: 对应数值4 w: 对应数...

Linux C编程学习笔记(1):chmod函数及文件的访问权限控制

#include #include #include   //基本系统数据类型 #include   //文件状态//argc记录了用户在运行程序的命令行中输入的参数的个数 //**argv:或...
  • hxllhhy
  • hxllhhy
  • 2017年06月09日 01:25
  • 93

【Unix/Linux】【命令】更改访问权限 —— chmod

Linux文件系统的最顶端是 / ,成为

利用chmod改变访问权限

chmod 用于改变文件或目录的访问权限。一种是包含字母和操作符表达式的字符设定法;另一种是包含数字的数字设定法。 1.字符设定法       chmod [who] [+|-|=] [mode]...
  • lzh_86
  • lzh_86
  • 2017年08月10日 09:03
  • 97

Android文件访问权限(第一个)

  • 2014年07月16日 16:58
  • 2.73MB
  • 下载

linux chmod命令更改文件的读写执行权限

linux chmod命令是在日常运维中比较常用的命令之一,对文件管理比较重要,如设置web目录时需设置特定的权限以保证服务器安全。 提示:在写完shell脚本后,我们一般需要给这脚本设置权限:c...

07_文件访问权限

  • 2017年07月27日 10:39
  • 1.13MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:_chmod, _wchmod 更改文件访问权限
举报原因:
原因补充:

(最多只允许输入30个字)