0
所以我必須做一個程序,執行結果與使用命令誰和我是誰在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;
}
您是否嘗試過使用調試器縮小發生分段錯誤的位置? –
*哪裏發生墜機?在調試器中運行以捕獲崩潰,當被捕獲時,您可以檢查並逐步調用函數調用堆棧(轉至* your * code)並檢查變量的值。至少請編輯您的問題,以顯示代碼中發生崩潰的位置以及相關變量的值。 –
你似乎忽略了'argc'的檢查,顯然你調用的程序參數太少。需要Mor信息。 –