计算机专业的就业前景:Unix环境高级编程_1.3 文件与目录 - c - 紫砂壶

来源:百度文库 编辑:九乡新闻网 时间:2024/04/28 17:43:59
Unix环境高级编程_1.3 文件与目录


文件条目
头文件 : 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);                //

起始目录
    由口令文件设置



struct dirent {
ino_t d_ino; /* 索引号 */
off_t d_off; /* 下一个偏移量 */
unsigned short d_reclen; /* 本记录长度 */
unsigned char d_type; /* 文件类型 */
char d_name[256]; /* 文件名 */
};提问人的追问  2010-01-29 10:06
能给我举个列子吗!
回答人的补充  2010-01-29 10:56 Unix环境高级编程_1.3 文件与目录


文件条目
头文件 : 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); //

起始目录
由口令文件设置

参考:
http://blog.chinaunix.net/u2/60220/showart_687858.html