#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

05-06 01:16