C語言中fgetgrent()函數(shù)和fgetpwent()函數(shù)的用法對比
C語言fgetgrent()函數(shù):讀取組格式函數(shù)
頭文件:
#include <grp.h> #include <stdio.h> #include <sys/types.h>
定義函數(shù):
struct group * getgrent(FILE * stream);
函數(shù)說明:fgetgrent()會從參數(shù)stream 指定的文件讀取一行數(shù)據(jù), 然后以group 結(jié)構(gòu)將該數(shù)據(jù)返回. 參數(shù)stream 所指定的文件必須和、etc/group 相同的格式. group 結(jié)構(gòu)定義請參考getgrent().
返回值:返回 group 結(jié)構(gòu)數(shù)據(jù), 如果返回NULL 則表示已無數(shù)據(jù), 或有錯誤發(fā)生.
范例
#include <grp.h> #include <sys/types.h> #include <stdio.h> main() { struct group *data; FILE *stream; int i; stream = fopen("/etc/group", "r"); while((data = fgetgrent(stream)) != 0) { i = 0; printf("%s :%s:%d :", data->gr_name, data->gr_passwd, data->gr_gid); while(data->gr_mem[i]) printf("%s, ", data->gr_mem[i++]); printf("\n"); } fclose(stream); }
執(zhí)行:
root:x:0:root, bin:x:1:root, bin, daemon daemon:x:2:root, bin, daemon sys:x:3:root, bin, adm adm:x:4:root, adm, daemon tty:x:5 disk:x:6:root lp:x:7:daemon, lp mem:x:8 kmem:x:9 wheel:x:10:root mail:x:12:mail news:x:13:news uucp:x:14:uucp man:x:15 games:x:20 gopher:x:30 dip:x:40: ftp:x:50 nobody:x:99:
C語言fgetpwent()函數(shù):讀取密碼格式
頭文件:
#include <pwd.h> #include <stdio.h> #include <sys/types.h>
定義函數(shù):
struct passwd * fgetpwent(FILE * stream);
函數(shù)說明:fgetpwent()會從參數(shù)stream 指定的文件讀取一行數(shù)據(jù), 然后以passwd 結(jié)構(gòu)將該數(shù)據(jù)返回. 參數(shù)stream 所指定的文件必須和/etc/passwd 相同的格式. passwd 結(jié)構(gòu)定義請參考getpwent().
返回值:返回 passwd 結(jié)構(gòu)數(shù)據(jù), 如果返回NULL 則表示已無數(shù)據(jù), 或有錯誤發(fā)生.
范例
#include <pwd.h> #include <sys/types.h> main() { struct passwd *user; FILE *stream; stream = fopen("/etc/passwd", "r"); while((user = fgetpwent(stream)) != 0) { printf("%s:%d:%d:%s:%s:%s\n", user->pw_name, user->pw_uid, user->pw_gid, user->pw_gecos, user->pw_dir, user->pw_shell); } }
執(zhí)行:
root:0:0:root:/root:/bin/bash bin:1:1:bin:/bin: daemon:2:2:daemon:/sbin: adm:3:4:adm:/var/adm: lp:4:7:lp:/var/spool/lpd: sync:5:0:sync:/sbin:/bin/sync shutdown:6:0:shutdown:/sbin:/sbin/shutdown halt:7:0:halt:/sbin:/sbin/halt mail:8:12:mail:/var/spool/mail: news:9:13:news:var/spool/news uucp:10:14:uucp:/var/spool/uucp: operator:11:0:operator :/root: games:12:100:games:/usr/games: gopher:13:30:gopher:/usr/lib/gopher-data: ftp:14:50:FTP User:/home/ftp: nobody:99:99:Nobody:/: xfs:100:101:X Font Server: /etc/Xll/fs:/bin/false gdm:42:42:/home/gdm:/bin/bash kids:500:500: : /home/kids:/bin/bash
上一篇:C語言中正切的相關(guān)函數(shù)總結(jié)
欄 目:C語言
本文標題:C語言中fgetgrent()函數(shù)和fgetpwent()函數(shù)的用法對比
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/2832.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改