Comparison of the use of fgetgrent () function and fgetpwent () function in C language _c language

Source: Internet
Author: User
Tags gopher

C language Fgetgrent () function: Reading group format functions

Header file:

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

To define a function:

struct Group * getgrent (FILE * stream);

Function Description: Fgetgrent () reads a row of data from the file specified by the parameter stream and returns the data in the group structure. The file specified by the parameter stream must be in the same format as, Etc/group. Group structure definition please refer to getgrent ().

Return value: Returns the group structure data, if returned null indicates no data, or an error occurred.

Example

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

Perform:

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 language Fgetpwent () function: reading password format

Header file:

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

To define a function:

struct passwd * fgetpwent (FILE * stream);

Function Description: Fgetpwent () reads a row of data from the file specified by the parameter stream, and then returns the data as a passwd structure. The file specified by the parameter stream must be in the same format as/etc/passwd. passwd structure definition refer to getpwent ().

Return value: Returns the PASSWD structure data, if returned null indicates no data, or an error occurred.

Example

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

Perform:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.