c - 使用 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);
解决方案
我想到了。正如我所说,我对这种编码东西很陌生,但是如果有人看到这个对于日志时间的自定义格式有类似的问题。你会想要制作一个文件并在那里打印当前的本地时间并关闭。然后在重新运行程序时读取该文件。下面只是一个示例,仅用于打开要写入的文件。
//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);
推荐阅读
- r - stringr 在 R 中处理棒球投球数据的问题
- macos - Vim 命令不再响应
- amazon-web-services - 当我的仪表板显示没有实例正在运行时,我如何连接到正在运行的 AWS 实例?
- swift - 创建依赖于外部 pod 的私有 pod 框架
- c++ - 在 C++ 中通过引用进行以下调用的 C 中的等效代码
- javascript - jQuery 在悬停时从新 URL 淡入新图像而不会出现白色“闪烁”
- c++ - FLTK 设置滑块值显示区域宽度
- python - Python GRPC 如何正确重用通道
- sql - {scientistSeedError: '“[”处或附近的语法错误'}
- python - 基于第二次出现在堆栈顶部的重复元素