Unix環(huán)境高級(jí)編程_1.3 文件與目錄
|
|
|
|
文件條目 頭文件 : dirent.h /usr/include/dirent.h
結(jié)構(gòu):DIR directory stream 該結(jié)構(gòu)用于定義路徑 包括:ino_t d_ino 文件編號(hào) 在<sys/types.h>中定義 char d_name[] 文件條目 文件名長(zhǎng)度不應(yīng)該超過(guò)NAME_MAX NAME_MAX 在limits.h中定義為: #define NAME_MAX 255
函數(shù): int closedir(DIR *dir) // 關(guān)閉賦給dir的路徑, 在此之后directory stream dir就不可用了 // 0 成功, -1出錯(cuò) DIR *opendir(const char *); struct dirent *readdir(DIR *dir); // 返回指向dirent結(jié)構(gòu)的指針 // 當(dāng)讀到最后一個(gè)文件,返回NULL
int readdir_r(DIR *restrict, struct dirent *restrict, struct dirent **restrict); void rewinddir(DIR *); void seekdir(DIR *, long); long telldir(DIR *);
結(jié)構(gòu): 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結(jié)構(gòu) 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中對(duì)應(yīng)類型的參數(shù) void va_end(va_list ap); // 當(dāng)調(diào)用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主要用于對(duì)于調(diào)用函數(shù)來(lái)說(shuō),不知道要傳入多少個(gè)參數(shù)和相應(yīng)類型。 必須先申明一個(gè)va_list對(duì)象類型,然后使用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 }
出錯(cuò)處理 頭文件 errno.h 在errno.h中定義整形變量errno,被系統(tǒng)用來(lái)調(diào)用出錯(cuò)事件。
頭文件 string.h strerror, strerror_r 用字符串來(lái)描述錯(cuò)誤號(hào)
頭文件 stdio.h int fflush(FILE *stream); # 強(qiáng)制清空stream的緩沖區(qū) # fflush(stdout)清空stdout的緩沖區(qū)了 stdin, stdout, stderr - 標(biāo)準(zhǔn)的IO流 #include <stdio.h> extern FILE *stdin; extern FILE *stdout; extern FILE *stderr;
int fputc(int c, FILE *stream); # 向stream流寫一個(gè)字符 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); // 修改當(dāng)前的工作目錄 int fchdir(int fd); //
起始目錄 由口令文件設(shè)置
|
|
struct dirent { ino_t d_ino; /* 索引號(hào) */ off_t d_off; /* 下一個(gè)偏移量 */ unsigned short d_reclen; /* 本記錄長(zhǎng)度 */ unsigned char d_type; /* 文件類型 */ char d_name[256]; /* 文件名 */ };
提問(wèn)人的追問(wèn)
2010-01-29 10:06
能給我舉個(gè)列子嗎!
回答人的補(bǔ)充
2010-01-29 10:56
Unix環(huán)境高級(jí)編程_1.3 文件與目錄
|
|
|
|
文件條目 頭文件 : dirent.h /usr/include/dirent.h
結(jié)構(gòu):DIR directory stream 該結(jié)構(gòu)用于定義路徑 包括:ino_t d_ino 文件編號(hào) 在<sys/types.h>中定義 char d_name[] 文件條目 文件名長(zhǎng)度不應(yīng)該超過(guò)NAME_MAX NAME_MAX 在limits.h中定義為: #define NAME_MAX 255
函數(shù): int closedir(DIR *dir) // 關(guān)閉賦給dir的路徑, 在此之后directory stream dir就不可用了 // 0 成功, -1出錯(cuò) DIR *opendir(const char *); struct dirent *readdir(DIR *dir); // 返回指向dirent結(jié)構(gòu)的指針 // 當(dāng)讀到最后一個(gè)文件,返回NULL
int readdir_r(DIR *restrict, struct dirent *restrict, struct dirent **restrict); void rewinddir(DIR *); void seekdir(DIR *, long); long telldir(DIR *);
結(jié)構(gòu): 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結(jié)構(gòu) 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中對(duì)應(yīng)類型的參數(shù) void va_end(va_list ap); // 當(dāng)調(diào)用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主要用于對(duì)于調(diào)用函數(shù)來(lái)說(shuō),不知道要傳入多少個(gè)參數(shù)和相應(yīng)類型。 必須先申明一個(gè)va_list對(duì)象類型,然后使用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 }
出錯(cuò)處理 頭文件 errno.h 在errno.h中定義整形變量errno,被系統(tǒng)用來(lái)調(diào)用出錯(cuò)事件。
頭文件 string.h strerror, strerror_r 用字符串來(lái)描述錯(cuò)誤號(hào)
頭文件 stdio.h int fflush(FILE *stream); # 強(qiáng)制清空stream的緩沖區(qū) # fflush(stdout)清空stdout的緩沖區(qū)了 stdin, stdout, stderr - 標(biāo)準(zhǔn)的IO流 #include <stdio.h> extern FILE *stdin; extern FILE *stdout; extern FILE *stderr;
int fputc(int c, FILE *stream); # 向stream流寫一個(gè)字符 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); // 修改當(dāng)前的工作目錄 int fchdir(int fd); //
起始目錄 由口令文件設(shè)置
參考: http://blog./u2/60220/showart_687858.html
|
|
|