//============================================================================
// Name : gcc_001.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
//获取当前目录
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h> //包含了Linux C 中的函数getcwd()
#define FILEPATH_MAX (80)
#include <iostream>
using namespace std;
void getTime(char* datetime, int type);
int main() {
const char date[512] = { 0 };
char hostname[512];
char *login;
char *usershell;
int pagesize;
long int hotsid = gethostid();
printf("%d,%d ok\n", getpid(), getuid());
cout << get_current_dir_name() << endl;
getdate(date);
cout << date << endl;
cout << getegid() << "," << geteuid() << endl;
printf("%d\n", hotsid);
gethostname(hostname, sizeof(hostname));
printf("%s\n", hostname);
login = getlogin();
pagesize = getpagesize();
usershell = getusershell();
printf("%d\n", pagesize);
printf("%s\n", usershell);
//printf("%s\n",login);
cout << "!!Hello World!!!" << endl; // prints !!!Hello World!!!
int result = access("test.txt", 0);
cout << result << endl;
char *file_path_getcwd;
file_path_getcwd = (char *) malloc(FILEPATH_MAX);
getcwd(file_path_getcwd, FILEPATH_MAX);
printf("%s\n", file_path_getcwd);
char datetime[64];
getTime(datetime, 0);
printf("%s\n", datetime);
getTime(datetime, 1);
printf("%s\n", datetime);
getTime(datetime, 2);
printf("%s\n", datetime);
getTime(datetime, 3);
printf("%s\n", datetime);
getTime(datetime, 4);
printf("%s\n", datetime);
return 0;
}
void getTime(char* datetime, int type) {
time_t now; //实例化time_t结构
struct tm *timenow; //实例化tm结构指针
time(&now);
//time函数读取现在的时间(国际标准时间非北京时间),然后传值给now
timenow = localtime(&now);
//localtime函数把从time取得的时间now换算成你电脑中的时间(就是你设置的地区)
//printf("%s", asctime(timenow));
switch (type) {
//1990-06-09 12:15:12
case 0:
sprintf(datetime, "%d-%d-%d %d:%d:%d", timenow->tm_year + 1900,
timenow->tm_mon + 1, timenow->tm_mday, timenow->tm_hour,
timenow->tm_min, timenow->tm_sec);
break;
case 1:
sprintf(datetime, "%d-%d-%d", timenow->tm_year + 1900,
timenow->tm_mon + 1, timenow->tm_mday);
break;
case 2:
sprintf(datetime, "%d:%d:%d", timenow->tm_hour, timenow->tm_min,
timenow->tm_sec);
break;
case 3:
sprintf(datetime, "%d/%d/%d %d:%d:%d", timenow->tm_year + 1900,
timenow->tm_mon + 1, timenow->tm_mday, timenow->tm_hour,
timenow->tm_min, timenow->tm_sec);
break;
case 4:
sprintf(datetime, "%d/%d/%d", timenow->tm_year + 1900,
timenow->tm_mon + 1, timenow->tm_mday);
break;
default:
break;
}
}
linux c 获取系统时间
最新推荐文章于 2022-12-15 14:02:25 发布