- #include <algorithm>
- #include <iterator>
- #include <iostream>
- #include <string>
- #include <sys/types.h>
- #include <pwd.h>
- #include <grp.h>
- #include <sys/utsname.h>
- #include <time.h>
- #include <unistd.h>
- int main(int argc, char* argv[])
- {
- struct utsname name;
- if (uname(&name) == -1)
- {
- perror("uname failure");
- return -1;
- }
- std::cout<<name.machine<<std::endl;
- std::cout<<name.sysname<<std::endl;
- std::cout<<name.version<<std::endl;
- std::cout<<name.release<<std::endl;
- char hostname[100];
- if (gethostname(hostname, sizeof(hostname)) == -1)
- {
- perror("gethostname failure");
- return -1;
- }
- std::cout<<hostname<<std::endl;
- time_t long_time;
- if (time(&long_time) == -1)
- {
- perror("time fialure");
- return -1;
- }
- std::cout<<long_time<<std::endl;
- struct tm* ptr_tm;
- if ((ptr_tm=localtime(&long_time)) == NULL)
- {
- perror("localtime fialure");
- return -1;
- }
- std::cout<<"current year is:"<<ptr_tm->tm_year+1900<<std::endl;//year since 1900
- std::cout<<"current month is:"<<ptr_tm->tm_mon+1<<std::endl;//mon is in[0, 11]
- std::cout<<"current day is:"<<ptr_tm->tm_mday<<std::endl;
- std::cout<<"current hour is:"<<ptr_tm->tm_hour<<std::endl;
- std::cout<<"current min is:"<<ptr_tm->tm_min<<std::endl;
- std::cout<<"current second is:"<<ptr_tm->tm_sec<<std::endl;
- std::cout<<"current weekday is:"<<ptr_tm->tm_wday<<std::endl;//wday since sunday[0, 6]
- }
unix/linux获取信息函数--例子
最新推荐文章于 2023-06-07 21:49:01 发布