UNIX环境高级编程——标准IO-实现查看所有用户

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
	char szBuf[512];
	char szName[512];
	FILE *f = fopen("/etc/passwd","r");
	memset(szBuf, 0, sizeof(szBuf));
	int i;
	while(fgets(szBuf, sizeof(szBuf),f) != NULL)
	{
		memset(szName, 0, sizeof(szName));
		for(i = 0; i< sizeof(szBuf); i++)
		{
			if(':' == szBuf[i])
			{
		   	   	break;
			}
			szName[i] = szBuf[i];
		}
		printf("%s 
", szName);
		memset(szBuf, 0, sizeof(szBuf));
	}
	fclose(f);
}

运行结果:

huangcheng@ubuntu:~$ ./a.out
root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp
proxy
www-data
backup
list
irc
gnats
nobody
libuuid
syslog
messagebus
avahi-autoipd
avahi
couchdb
speech-dispatcher
usbmux
haldaemon
kernoops
pulse
rtkit
saned
hplip
gdm
huangcheng
tftp
statd
sshd
samba
ftp

原文地址:https://www.cnblogs.com/wangfengju/p/6172500.html