我正在做一项任务,涉及使用各种数据结构来存储和排序数据。数据结构部分对我来说是相当直接的,然而,我在读取/解析wtmp文件的第一步就挂起了。我以前解析过几个文件,但从来没有解析过二进制文件。解析这样一个文件的诀窍是什么?提前谢谢。
发布于 2012-04-26 05:43:45
尝试使用utmp.h。
发布于 2012-04-26 06:36:40
这是一个如何解析它的例子,你需要将文件的字节读入utmp结构中。你真的应该检查手册页面的所有细节,这与用标准C库读取文件不同。
#include<stdio.h>
#include<fcntl.h>
#include<utmp.h>
int main()
{
int fd;
struct utmp cr;
int reclen = sizeof(struct utmp);
fd = open(WTMP_FILE, O_RDONLY);
if (fd == -1){
perror("oops");
exit(1);
}
while (read(fd, &cr, reclen) == reclen)
printf("-- %s\n", cr.ut_user);
close (fd);
return 0;
}https://stackoverflow.com/questions/10324144
复制相似问题