标题:Segmentation fault (core dumped)
只看楼主
GreasyFish
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2017-7-27
 问题点数:0 回复次数:3 
Segmentation fault (core dumped)

#include<stdio.h>
#include<utmp.h>
#include<fcntl.h>
#include<unistd.h>

#define SHOWHOST            /* include remote machine on output */

int main()
{
        struct utmp current_record;     /* read info int here */
        int         utmpfd;             /* read from this descriptor */
        int         reclen = sizeof(current_record);

        if(( utmpfd = open(UTMP_FILE, O_RDONLY)) == -1) {
                perror(UTMP_FILE);      /* UTMP_FILE is in utmp.h */
                exit(1);
        }

        while(read(utmpfd, &current_record, reclen) == reclen)
                show_info(&current_record);
        close(utmpfd);
        return 0;                       /* went ok */
}
show_info(struct utmp *utbufp)
{
        if( utbufp->ut_type != USER_PROCESS )
                return;
        printf("% -8.8s", utbufp->ut_name);     /* the logname */
        printf(" ");                            /* a space */
        printf("% -8.8s", utbufp->ut_line);     /* the tty */
        printf(" ");
        showtime(utbufp->ut_time);
    #ifdef SHOWHOST
        printf("( % s)", utbufp->ut_host);      /* the host */
    #endif
        printf("\n");                           /* new line */
}

void showtime( long timeval )
{
        char *cp;
        cp = ctime(&timeval);
        printf("%12.12s",cp+4);
}
代码如上,调试问题如下:

有大神给个回复嘛,系统是ubuntu16.04,gcc编译

搜索更多相关主题的帖子: Segmentation include int printf the 
2017-07-27 17:06
虾米小小小
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:30
专家分:135
注 册:2017-7-22
得分:0 
utmp.h文件呢?

2017-07-27 18:50
GreasyFish
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2017-7-27
得分:0 
回复 2楼 虾米小小小
utmp.h文件

你的意思是字长的问题?
2017-07-28 08:58
GreasyFish
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2017-7-27
得分:0 
回复 2楼 虾米小小小
解决了,谢谢
2017-07-28 09:10



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-479660-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.196812 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved