UNIX环境高级编程学习之第五章标准IO-实现查看所有用户
[code lang="cpp"]#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);
}
[/code]
文章评论