C語言對組檔案處理的相關函數小結_C 語言

來源:互聯網
上載者:User

C語言getgrent()函數:從組檔案中取得帳號的資料
標頭檔:

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

定義函數:

struct group * getgrent(void);

函數說明:getgrent()用來從組檔案(/etc/group)中讀取一項組資料, 該資料以group 結構返回. 第一次調用時會取得第一項組資料, 之後每調用一次就會返回下一項資料, 直到已無任何資料時返回NULL.

struct group{  char *gr_name; //組名稱  char *gr_passwd; //組密碼  gid_t gr_gid; //組識別碼  char **gr_mem; //群組成員帳號}

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

附加說明:getgrent()在第一次調用時會開啟組檔案, 讀取資料完畢後可使用endgrent()來關閉該組檔案.

錯誤碼:
ENOMEM:記憶體不足, 無法配置group 結構。

範例

#include <grp.h>#include <sys/types.h>main(){  struct group *data;  int i;  while((data = getgrent()) != 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");  }  endgrent();}

執行:

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, daemontty:x:5disk:x:6:rootlp:x:7:daemon, lpmem:x:8kmem:x:9:wheel:x:10:rootmail:x:12:mailnews:x:13:newsuucp:x:14:uucpman:x:15:games:x:20gopher:x:30dip:x:40ftp:x:50nobody:x:99

C語言setgrent()函數:從頭讀取組檔案中的組資料
標頭檔:

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

定義函數:

void setgrent(void);

函數說明:setgrent()用來將getgrent()的讀寫地址指回組檔案開頭。

用法參考 setpwent().

C語言endgrent()函數:關閉檔案(關閉組檔案)
相關函數:

getgrent, setgrent

標頭檔:

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

定義函數:

void endgrent(void);

函數說明:endgrent()用來關閉由getgrent()所開啟的密碼檔案。

範例請參考setgrent().

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.