函数简介函数原型:
int _chmod(
const char *filename,
int pmode
);
函数功能:改变文件的读写许可设置,如果改变成功返回0,否则返回-1
所属库:io.h
相关函数:_tchmod 、_wchmod
程序示例#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
// Change the mode and report error or success
void set_mode_and_report(char * filename, int mask)
{
// Check for failure
if( _chmod( filename, mask ) == -1 )
{
// Determine cause of failure and report.
switch (errno)
{
case EINVAL:
fprintf( stderr, "Invalid parameter to chmod.
");
break;
case ENOENT:
fprintf( stderr, "File %s not found
", filename );
break;
default:
// Should never be reached
fprintf( stderr, "Unexpected error in chmod.
" );
}
}
else
{
if (mask == _S_IREAD)
printf( "Mode set to read-only
" );
else if (mask & _S_IWRITE)
printf( "Mode set to read/write
" );
}
fflush(stderr);
}
int main( void )
{
// Create or append to a file.
system( "echo /* End of file */ >> crt_chmod.c_input" );
// Set file mode to read-only:
set_mode_and_report("crt_chmod.c_input ", _S_IREAD );
system( "echo /* End of file */ >> crt_chmod.c_input " );
// Change back to read/write:
set_mode_and_report("crt_chmod.c_input ", _S_IWRITE );
system( "echo /* End of file */ >> crt_chmod.c_input " );
}