分割错误(核心转储)C linux
问题描述:
所以我必须做一个程序,执行结果与使用命令谁和我是谁在Linux后类似。问题是这两个函数里面的if(strcmp ...和其他...正在分离时工作)分割错误(核心转储)C linux
主要问题是我必须将它们都放在一个文件中,并且它不起作用。放在一起只是做一些事情./程序是我的,而./程序告诉分割错误(核心转储)
第二个问题是,我不知道如何使部分./program我工作正常它应该返回我只有我:用户PTS/0日期时间的...不是所有用户都喜欢在别人......部分
#include <stdio.h>
#include <utmp.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <sys/types.h>
#define SHOWHOST
void show_info_who(struct utmp *utbufp)
{
if(utbufp->ut_type > 4){
time_t czas = utbufp->ut_time;
char buf[80];
struct tm* timeinfo = localtime(&czas);
printf("%-8.8s", utbufp->ut_name);
printf(" ");
printf("%-8.8s", utbufp->ut_line);
printf(" ");
strftime(buf, 80, "%F %R" , timeinfo);
printf("%s", buf);
printf(" ");
#ifdef SHOWHOST
printf("(%s)", utbufp->ut_host);
#endif
printf("\n");
}
}
int main(int argc, char *argv[])
{
struct utmp current_record;
int utmpfd;
int reclen = sizeof(current_record);
if ((utmpfd = open(UTMP_FILE, O_RDONLY)) == -1){
perror(UTMP_FILE);
exit(1);
}
if(strcmp (argv[1], "am") == 0){
if(strcmp (argv[2], "i") == 0){
while (read(utmpfd, ¤t_record, reclen) == reclen)
show_info_who(¤t_record);
printf("test\n");
}
close(utmpfd);
return 0;
}
while (read(utmpfd, ¤t_record, reclen) == reclen)
show_info_who(¤t_record);
close(utmpfd);
return 0;
}
答
你为什么不尝试使用,
。void setutent(void); // for rewinding
struct utmp *getutent(void);
不推荐直接打开并查找utmp文件。 一个utmp文件似乎是一个可以*访问的普通文件,但它的行为就像一个数据库。
您是否尝试过使用调试器缩小发生分段错误的位置? –
*哪里发生坠机?在调试器中运行以捕获崩溃,当被捕获时,您可以检查并逐步调用函数调用堆栈(转至* your * code)并检查变量的值。至少请编辑您的问题,以显示代码中发生崩溃的位置以及相关变量的值。 –
你似乎忽略了'argc'的检查,显然你调用的程序参数太少。需要Mor信息。 –