C語言中fgetgrent()函數和fgetpwent()函數的用法對比_C 語言

來源:互聯網
上載者:User

C語言fgetgrent()函數:讀取組格式函數

標頭檔:

#include <grp.h>  #include <stdio.h>  #include <sys/types.h>

定義函數:

struct group * getgrent(FILE * stream);

函數說明:fgetgrent()會從參數stream 指定的檔案讀取一行資料, 然後以group 結構將該資料返回. 參數stream 所指定的檔案必須和、etc/group 相同的格式. group 結構定義請參考getgrent().

傳回值:返回 group 結構資料, 如果返回NULL 則表示已無資料, 或有錯誤發生.

範例

#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);}

執行:

root:x:0:root,bin:x:1:root, bin, daemondaemon:x:2:root, bin, daemonsys:x:3:root, bin, admadm:x:4:root, adm, daemontty:x:5disk:x:6:rootlp:x:7:daemon, lpmem:x:8kmem:x:9wheel:x:10:rootmail:x:12:mailnews:x:13:newsuucp:x:14:uucpman:x:15games:x:20gopher:x:30dip:x:40:ftp:x:50nobody:x:99:

C語言fgetpwent()函數:讀取密碼格式

標頭檔:

#include <pwd.h>  #include <stdio.h>  #include <sys/types.h>

定義函數:

struct passwd * fgetpwent(FILE * stream);

函數說明:fgetpwent()會從參數stream 指定的檔案讀取一行資料, 然後以passwd 結構將該資料返回. 參數stream 所指定的檔案必須和/etc/passwd 相同的格式. passwd 結構定義請參考getpwent().

傳回值:返回 passwd 結構資料, 如果返回NULL 則表示已無資料, 或有錯誤發生.

範例

#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);  }}

執行:

root:0:0:root:/root:/bin/bashbin: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/syncshutdown:6:0:shutdown:/sbin:/sbin/shutdownhalt:7:0:halt:/sbin:/sbin/haltmail:8:12:mail:/var/spool/mail:news:9:13:news:var/spool/newsuucp: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/falsegdm:42:42:/home/gdm:/bin/bashkids:500:500: : /home/kids:/bin/bash

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.