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 \n", 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

posted on 2013-08-03 02:03  疯子123  阅读(144)  评论(0编辑  收藏  举报

导航