Chinaunix首页 | 论坛 | 博客
  • 博客访问: 477998
  • 博文数量: 115
  • 博客积分: 5016
  • 博客等级: 大校
  • 技术积分: 1401
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-21 16:03
文章分类

全部博文(115)

文章存档

2013年(1)

2010年(17)

2009年(76)

2008年(21)

我的朋友

分类: LINUX

2008-12-10 11:36:24

1、stat族函数 #include
int stat(const char *restrict pathname,struct stat *restrict buf); 
int lstat(const char *restrict pathname,struct stat *restrict buf);
int fstat(int fildes,struct stat *buf); 
成功返回0,失败则返回-1. 
注意stat和lstat的区别仅仅在于处理的文件是符号链接时,lstat返回的是富含连接的有关信息,而stat则返回符号链接引用文件的信息。
关于结构体stat: 
(1)文件类型 st_mode (实际等同于file_type&mode)可以通过一系列的宏来确定,这些宏的参数都是stat结构中的st_mode成员。这些宏都有:S_IS(REG|DIR|CHR|BLK|FIFO|LNK|SOCK),它们的定义方式如下: #define S_ISDIR(mode) (((mode)&S_IFDIR)==S_IFDIR) 
注意:对于进程通信对象是通过以下宏来确定IPC的类型: S_TYPEIS(MQ|SEM|SHM)分别是消息队列、信号量、共享存储对象。它们的参数是指向stat的指针。 (
2)st_uid和st_gid分别表示文件的所有者和组所有者在此注意于每个进程相关联的用户ID和组ID 实际用户/组ID 表示我们实际上是谁有效用户/组ID和附加组ID 用于文件访问权限检查保存的设置用户/组ID 由exec函数保存 
(3)文件的访问权限宏定义与open函数的mode参数一样 
(4)access函数 
#include 
int access(const char *pathname,int mode);
按实际的用户/组ID进行访问权限设置成功返回0失败则返回-1 mode参数(按位或):R_OK W_OK X_OK F_OK 分别测试读写执行权限和测试文件是否存在 
(5)umask函数 
#include
mode_t umask(mode_t cmask); 
返回当前文件模式创建屏蔽字,对于任何在文件模式创建屏蔽字中为1的位,在文件mode中的相应位则一定被关闭。注意:umask函数为进程设置文件模式创建屏蔽字,但是返回的是以前的值!符号形式的umask命令与八进制形式的不同,符号格式指定许可的权限而非拒绝的权限! 
(6)chmod和fchmod函数 
#include
int chmod(const char *pathname,mode_t mode); 
int fchomd(int fildes,mode_t mode); 
比较stat和fstat记忆,对于chmod的mode参数除了open函数中支持的以外还有S_IRWX[UGO] 
(7)chown、fchown和lchown函数 
#include 
int chown(const char *pathname,uid_t owner,gid_t group); 
int lchown(const char *pathname,uid_t owner,gid_t group); 
int fchown(int fildes,uid_t owner,gid_t group); 
若成功就返回0,失败则返回-1.chown和lchown的区别也仅仅在符号链接时,lchown更改的是符号链接本身的所有者,而chown更改的是符号链接指向的文件。 
(8)文件长度 stat的结构成员st_size表示以字节为单位的文件长度。此字段只对普通文件,目录文件和符号链接起作用。 
(9)文件截短 #include 
int truncate(const char *pathname,off_t length); 
int ftruncate(int fildes,off_t length); 
成功返回0,失败则返回-1. 这两个函数将把现在的文件长度截短为length字节。 
(10)link、unlink、remove和rename函数 
#include 
int link(const char *exitingpath,const char *newpath); 
引用现在的文件exitingpath创建一个新的newpath。 
int unlink(const char *pathname); 
此函数删除一个目录项,并将由pathname所引用文件的连接技术减1. 成功返回0 失败返回-1 
#include
int remove(const char *pathname); 
int rename(const char *oldname,const char *newname); 
这两个函数都是成功返回0,失败返回-1. 
remove对文件操作和unlink的作用相同,对目录操作和rmdir功能相同。 
(11)symlink和readlink函数 #include 
int symlink(const char *actualpath,const char *sympath);
用于创建一个指向actualpath的符号链接sympath。若成功则返回0,出错则返回-1. 
#include 
int readlink(const char *restrict pathname,char *restrict buf,zize_t buftype); readlink()会将参数path的符号连接内容存到参数buf所指的内存空间,返回的内容不以NULL作字符串结尾,但会将字符串的字符数返回。若参数bufsiz小于符号连接的内容长度,过长的内容会被截断。
返回值执行成功则传符号连接所指的文件路径字符数,失败则返回-1,错误代码存于errno。错误代码 EACCESS 取文件时被拒绝,权限不够 EINVAL 参数bufsiz 为负数 EIO I/O 存取错误。 ELOOP 欲打开的文件有过多符号连接问题。 ENAMETOOLONG 参数path的路径名称太长 ENOENT 参数path所指定的文件不存在 ENOMEM 核心内存不足 ENOTDIR 参数path路径中的目录存在但却非真正的目录。
晕死了,今天写了一个程序来实践本章的知识,却因为一个小小的问题调试了一下午,把错误放在这里,希望能给看到的人以提示!也警戒自己!不过弄明白了一点,就是readlink的参数是链接文件名,返回的是被链接文件的文件名字符数,存到buf中的是被链接文件的名字,而且不以NULL结尾! #include
#include
#include
#include
#include
#include
int main()
{
 int n;
 int fildes;
 char buf[100];
 char *writebuf = "I am xlt!";
 if((fildes = open("tempfile",O_CREAT|O_RDWR|O_TRUNC,S_IRUSR|S_IWUSR|S_IXUSR))<=0)
 {
  printf("File create fail!\n");
 }
 printf("%d\n",fildes);
 if(write(fildes,writebuf,10)!=10)
 {
  printf("Write error!\n");
 }
 if(symlink("tempfile","tempfile1")<0)
  printf("Link fail!\n");
 if(n=readlink("tempfile1",buf,100)<0)
 {
  printf("read link error!\n");
  exit(1);
 }
 printf("The value of n is %d\n",n);
 fsync(fildes);
 buf[n]='\0';
 printf("the linkfile is %s\n",buf);
 if(rename("tempfile1","tempfile2")<0)
 {
  printf("Rename error!\n");
  exit(1);
 }
 if(n=readlink("tempfile2",buf,20)<0)
 {
  printf("read link error!\n");
  exit(1);
 }
 buf[n]='\0';
 printf("the linkfile is %s\n",buf);
 if(unlink("tempfile")<0)
 {
  printf("unlink error!\n");
  exit(1);
 }
 sleep(5);
 printf("Done!\n");
 if((n=readlink("tempfile2",buf,20))<0)
 {
  printf("read link error!\n");
  exit(1);
 }
 buf[n]='\0';
 printf("the linkfile is %s\n",buf);
 exit(0);
}
结论:赋值符比比较运算符的优先级低!其实《C陷阱和缺陷》真的很好!当时看书时没有好好看,现在才感觉很多东西都是在吃了亏之后才知道它的可贵!有时间再重温一下吧!
(12)文件时间及utime函数
每个文件都保持有三个字段 
st_atime表示文件数据的最后访问时间 
st_mtime表示文件数据的最后修改时间 
st_ctime表示i节点状态的最后更改时间 
一个文件的访问时间和修改时间可以用utime函数修改 
#include
int utime(const char *pathname,const struct utimbuf *times);成功返回0 出错返回-1 此函数所使用的数据结构是: 
struct utimbuf{ time_t actime; 访问时间 time_t modtime; 修改时间 } 
(13)目录操作 #include 
int mkdir(const char *pathname,mode_t mode); 创建一个新目录 
int rmdir(const char *pathname); 删除目录 
这两个函数成功则返回0,出错则返回-1 
读目录: #include 
DIR *opendir(const char *parhname); 成功则返回指针 若出错则返回NULL 
struct dirent *readdir(DIR *dp); 成功则返回指针 若在目录结尾或出错则返回NULL 
void rewinddir(DIR *dp); int closedir(DIR *dp); 成功则返回0 出错则返回-1 
long telldir(DIR *dp); 返回与dp关联的目录中的当前位置 
void seekdir(DIR *dp,long loc); 
struct dirent{ 
ino_t d_ino; /*i-node number*/ 
char d_name[NAME_MAX + 1]; /*null-terminated filename*/ 
}
(14)
chdir、fchdir和getcwd函数
#include
int chdir(const char *pathname);
int fchdir(int filedes);
若成功返回0,出错则返回-1.
char *getcwd(char *buf,size_t size);
若成功返回buf,出错则返回NULL。
阅读(1098) | 评论(0) | 转发(0) |
0

上一篇:APUE学习之1文件I/O

下一篇:计划与目标

给主人留下些什么吧!~~