Jason's Blog

L & Cod 

C中的access函数

 

int   access(const   char   *filename,   int   amode); 
amode参数为0时表示检查文件的存在性,如果文件存在,返回0,不存在,返回-1。 
这个函数还可以检查其它文件属性: 
06     检查读写权限 
04     检查读权限 
02     检查写权限 
01     检查执行权限 
00     检查文件的存在性
而这个就算这个文件没有读权限,也可以判断这个文件存在于否
存在返回0,不存在返回-1

C函数
  函数名: access 
  功 能: 确定文件的访问权限 
  用 法: int access(const char *filename, int amode);
[编辑本段]access
  Synopsis
  #include <io.h>
  int _access(const char *path,int mode) ;
  Description
  The access function, when used with files, determines whether the specified file exists and can be accessed as specified by the value of mode. When used with directories, _access determines only whether the specified directory exists; since under Windows all directories have read and write access.
  The mode argument can be one of :
  00 Existence only
  02 Write permission
  04 Read permission
  06 Read and write permission 
  Returns
  Zero if the file has the given mode, -1 if an error occurs.
  Portability :
  Windows. Under Unix a similar function exists too.
  Note that lcc-win32 accepts both _access (Microsoft convention) and access.
  程序例: 
  

  1. #include <stdio.h> 
  2.   #include <io.h> 
  3.   int file_exists(char *filename); 
  4.   int main(void) 
  5.   { 
  6.   printf("Does NOTEXIST.FIL exist: %s\n", 
  7.   file_exists("NOTEXISTS.FIL") ? "YES" : "NO"); 
  8.   return 0; 
  9.   } 
  10.   int file_exists(char *filename) 
  11.   { 
  12.   return (access(filename, 0) == 0); 
  13.   }

 

常量指针与指针常量

  首先,我告诉大家一个小规则,就是像这样连着的两个词,前面的一个通常是修饰部分,中心词是后面一个词,怎么说呢。就像这里的常量指针和指针常量。

  常量指针,表述为“是常量的指针”,它首先应该是一个指针。
  指针常量,表述为“是指针的常量”,它首先应该是一个常量。

   我再分开细细说明,常量指针,它是一个指针,什么样的指针呢?它是一个指向常量的指针,就是说我们定义了一个常量,比如 const int a=7; 那么我们就可以定义一个常量指针来指向它 const int *p=&a; 也可以分成两步,即 const int *p; p=&a; 那么它有什么作用呢?首先我们来说说常量的属性,因为我们的指针是指向常量的,常量和变量的变量的不同之处在于我们不能对其内容进行 操作,具体说就是修改,而我们的指针是什么,它的内容本身是一个地址,设置常量指针指向一个常量,为的就是防止我们写程序过程中对指针误操作出现了修改常 量这样的错误,应该如果我们修改常量指针的所指向的空间的时候,编译系统就会提示我们出错信息。总结一下,常量指针就是指向常量的指针,指针所指向的地址的内容是不可修改的。

  再 来说说指针常量,它首先是一个常量,再才是一个指针。常量的性质是不能修改,指针的内容实际是一个地址,那么指针常量就是内容不能修改的常量,即内容不能 修改的指针,指针的内容是什么呀?指针的内容是地址,所以,说到底,就是不能修改这个指针所指向的地址,一开始初始化,指向哪儿,它就只能指向哪儿了,不 能指向其他的地方了,就像一个数组的数组名一样,是一个固定的指针,不能对它移动操作,比如你使用 p++; 系统就会提示出错。但是它只是不能修改它指 向的地方,但这个指向的地方里的内容是可以替换的,这和上面说的常量指针是完全不同的概念。作一下总结,指针常量就是是指针的常量,它是不可改变地址的指针,但是可以对它所指向的内容进行修改。对了,忘了说说它怎么用,举个小例子 int a; int * const p=&a; 也可以分开写 int a; int * const p; p=&a;

  当然,你也可以定义个一个指向常量的指针常量,就把上面的两个综合一下,表示如下

const int a=7; const int * const p=&a;

  多上机试试,多揣摩一下,其实并不是很难理解。而且用我的方法也很好记,不是吗,呵呵。