首页 > 解决方案 > 使用 unix 打印用户上次登录时间

问题描述

我对编码相当陌生,但在这里我在 putty (UNIX) 中使用了一个 .c 文件。下面的打印是我目前正在使用的,它以 [YYYY:MM:DD:HR:MN:SEC] 的格式打印出当前的本地时间。当我运行客户端程序时,我需要从服务器打印客户端当前用户的上次日志时间。使用相同的格式 [YYYY:MM:DD:HR:MN:SEC]。(需要制作自定义客户端/服务器代码以上述格式打印,因为 shell 在使用最后一个命令时会打印出工作日、月份、月份 #、小时:分钟)。

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h> 
#include <time.h>

#define     DAYSIZE 80
#define     Month 12
#define RCVBUFSIZE 1024   
#define SNDBUFSIZE 1024  



fprintf(stderr, "The last time connected [%d:%s:%d:%2d:%2d:%2d] is the last time when the client program disconnected from the server using the Quit command \n",year,m,wd,hr,mn,sc);

标签: cunix-socket

解决方案


我想到了。正如我所说,我对这种编码东西很陌生,但是如果有人看到这个对于日志时间的自定义格式有类似的问题。你会想要制作一个文件并在那里打印当前的本地时间并关闭。然后在重新运行程序时读取该文件。下面只是一个示例,仅用于打开要写入的文件。

//open file name Log.txt then writes in it
fp  = fopen ("Log.txt", "w");   

//what is going inside file
fprintf(fp, "The last time connected [%d:%s:%d:%2d:%2d:%2d]",year,m,wd,hr,mn,sc);  
fclose (fp);

推荐阅读