C語言中fgetgrent()函數(shù)和fgetpwent()函數(shù)的用法對(duì)比
C語言fgetgrent()函數(shù):讀取組格式函數(shù)
頭文件:
#include <grp.h> #include <stdio.h> #include <sys/types.h>
定義函數(shù):
struct group * getgrent(FILE * stream);
函數(shù)說明:fgetgrent()會(huì)從參數(shù)stream 指定的文件讀取一行數(shù)據(jù), 然后以group 結(jié)構(gòu)將該數(shù)據(jù)返回. 參數(shù)stream 所指定的文件必須和、etc/group 相同的格式. group 結(jié)構(gòu)定義請(qǐng)參考getgrent().
返回值:返回 group 結(jié)構(gòu)數(shù)據(jù), 如果返回NULL 則表示已無數(shù)據(jù), 或有錯(cuò)誤發(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()會(huì)從參數(shù)stream 指定的文件讀取一行數(shù)據(jù), 然后以passwd 結(jié)構(gòu)將該數(shù)據(jù)返回. 參數(shù)stream 所指定的文件必須和/etc/passwd 相同的格式. passwd 結(jié)構(gòu)定義請(qǐng)參考getpwent().
返回值:返回 passwd 結(jié)構(gòu)數(shù)據(jù), 如果返回NULL 則表示已無數(shù)據(jù), 或有錯(cuò)誤發(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
相關(guān)文章
C語言實(shí)現(xiàn)進(jìn)程5狀態(tài)模型的狀態(tài)機(jī)
狀態(tài)機(jī)在實(shí)際工作開發(fā)中應(yīng)用非常廣泛,用這幅圖就可以很清晰的表達(dá)整個(gè)狀態(tài)的流轉(zhuǎn)。本篇通過C語言實(shí)現(xiàn)一個(gè)簡單的進(jìn)程5狀態(tài)模型的狀態(tài)機(jī),讓大家熟悉一下狀態(tài)機(jī)的魅力,需要的可以參考一下2022-10-10C++中POCO庫的安裝與基礎(chǔ)知識(shí)介紹(Windwos和Linux)
這篇文章主要為大家介紹了C++ POCO庫的簡單介紹、下載以及安裝方式、簡單代碼示例,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-05-05C++?std::chrono庫使用示例(實(shí)現(xiàn)C++?獲取日期,時(shí)間戳,計(jì)時(shí)等功能)
std::chrono是C++標(biāo)準(zhǔn)庫中的一個(gè)組件,用于表示和處理時(shí)間,這篇文章主要介紹了C++?std::chrono庫使用指南(實(shí)現(xiàn)C++?獲取日期,時(shí)間戳,計(jì)時(shí)等功能),需要的朋友可以參考下2023-06-06Qt利用tablewidget模擬手指實(shí)現(xiàn)滑動(dòng)
這篇文章主要為大家詳細(xì)介紹了Qt如何利用tablewidget模擬手指實(shí)現(xiàn)滑動(dòng)效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Qt有一定的幫助,需要的可以參考一下2023-01-01