Chinaunix首页 | 论坛 | 博客
  • 博客访问: 526931
  • 博文数量: 128
  • 博客积分: 4000
  • 博客等级: 上校
  • 技术积分: 1345
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-22 21:43
文章分类

全部博文(128)

文章存档

2009年(30)

2008年(98)

我的朋友

分类: LINUX

2008-05-14 13:57:45

文件条目
头文件 : dirent.h
    /usr/include/dirent.h

结构:DIR        directory stream 该结构用于定义路径
    包括:ino_t  d_ino                文件编号    在中定义     
            char   d_name[]            文件条目    文件名长度不应该超过NAME_MAX
                                                NAME_MAX 在limits.h中定义为: #define NAME_MAX  255

函数:                                               
    int             closedir(DIR *dir)                                      // 关闭赋给dir的路径, 在此之后directory stream dir就不可用了
                                                                            // 0 成功, -1出错
    DIR             *opendir(const char *);
    struct dirent   *readdir(DIR *dir);                                     // 返回指向dirent结构的指针
                                                                            // 当读到最后一个文件,返回NULL

    int             readdir_r(DIR *restrict, struct dirent *restrict,
                            struct dirent **restrict);
    void            rewinddir(DIR *);
    void            seekdir(DIR *, long);
    long            telldir(DIR *);

结构:
    struct dirent {
              ino_t          d_ino;       /* inode number */
              off_t          d_off;       /* offset to the next dirent */
              unsigned short d_reclen;    /* length of this record */
              unsigned char  d_type;      /* type of file */
              char           d_name[256]; /* filename */
          };

例子:
    DIR             *dp;
    struct dirent   *dirp;

    if((dp = opendir(argv[1])) == NULL)
        err_sys("can't open %s", argv[1]);

    while((dirp = readdir(dp)) != NULL)            // 读取dp下所有的dirent结构
        printf("%s\n", dirp->d_name);

    closedir(dp);


头文件 stdarg.h
       void va_start(va_list ap, last);
       type va_arg(va_list ap, type);                // 获取va_list中对应类型的参数
       void va_end(va_list ap);                        // 当调用va_end后,ap就变成未定义
       void va_copy(va_list dest, va_list src);        // va_list aq;
                                                      // va_copy(aq, ap);
                                                      // ...
                                                      // va_end(aq);

在stdarg.h中定义va_list主要用于对于调用函数来说,不知道要传入多少个参数和相应类型。
必须先申明一个va_list对象类型,然后使用va_start(), va_arg(), 和 va_end()宏

例子:
 56 /* Fatal error unrelated to a system call.
 57  * Print a message and terminate. */
 58 void err_quit(const char *fmt, ...)
 59 {
 60     va_list     ap;
 61     va_start(ap, fmt);
 62     err_doit(0, fmt, ap);
 63     va_end(ap);
 64     exit(1);
 65 }


出错处理
头文件  errno.h
    在errno.h中定义整形变量errno,被系统用来调用出错事件。



头文件 string.h
    strerror, strerror_r 用字符串来描述错误号

头文件 stdio.h
    int fflush(FILE *stream);            # 强制清空stream的缓冲区
                                        # fflush(stdout)清空stdout的缓冲区了
    stdin, stdout, stderr - 标准的IO流
     #include
     extern FILE *stdin;
     extern FILE *stdout;
     extern FILE *stderr;

       int fputc(int c, FILE *stream);                # 向stream流写一个字符
       int fputs(const char *s, FILE *stream);        # 向stream流写字符串,不包括'\0'
       int putc(int c, FILE *stream);                # 向stream流写一字符,等同于fputc
       int putchar(int c);                            # 等同于putc(c, stdout)
       int puts(const char *s);                        # 向stdout输入字符串和回行符(newline)


例:
 67 /* Print a message and return to caller.
 68  * Caller specifies "errnoflag". */
 69 static void err_doit(int errnoflag, const char *fmt, va_list ap)
 70 {
 71     int     errno_save;
 72     char    buf[MAXLINE];
 73
 74     errno_save = errno;                 /* value caller might want printed */
 75     vsprintf(buf, fmt, ap);
 76     if (errnoflag)
 77         sprintf(buf + strlen(buf), ": %s", strerror(errno_save));
 78     strcat(buf, "\n");
 79     fflush(stdout);                     /* in case stdout and stderr are the same */
 80     fputs(buf, stderr);
 81     fflush(NULL);                       /* flushes all stdio output streams */
 82     return;
 83 }


工作目录
头文件unisdt.h
       int chdir(const char *path);        // 修改当前的工作目录
       int fchdir(int fd);                //

起始目录
    由口令文件设置

阅读(1346) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~