Chinaunix
标题:
请问linux下C编程有没有获取系统当前时间的函数?
[打印本页]
作者:
``````
时间:
2006-03-31 10:04
标题:
请问linux下C编程有没有获取系统当前时间的函数?
请各位高手赐教:
linux下的C编程中获取当前系统时间的函数是哪个?
另还有一个问题:如何修改linux系统的IP,也是用C语言实现。
小弟初学linux编程,很多不懂,请高手们指教!
多谢了!
作者:
mq110
时间:
2006-03-31 15:21
man gettimeofday
改IP的话.用ioctl的方式.
int set_dev_ip(char *ip,char *ethname)
{
struct ifreq ifreq;
struct protoent *pro;
struct sockaddr_in ipaddr;
int sockfd;
pro=getprotobyname("tcp");
if((sockfd=socket(AF_INET,SOCK_STREAM,pro->p_proto))<0)
{
perror("socket");
return -1;
}
strcpy(ifreq.ifr_name,ethname);
if(ioctl(sockfd,SIOCGIFADDR,&ifreq)<0)
{
perror("ioctl");
return -2;
}
bcopy(&ifreq.ifr_addr,&ipaddr,sizeof(ifreq.ifr_addr));
ipaddr.sin_addr.s_addr=inet_addr(ip);
bcopy(&ipaddr,&ifreq.ifr_addr,sizeof(ipaddr));
if(ioctl(sockfd,SIOCSIFADDR,&ifreq)<0)
{
perror("ioctl");
return -3;
}
return 0;
}
复制代码
自己改吧.~
作者:
handi
时间:
2006-03-31 17:11
系统时钟还是系统运行时间?
作者:
adm1ner
时间:
2006-03-31 23:08
time_t tp;
char str;
time(&tp); 获得1970年1月1日到现在的秒数,一个很大的值
str=ctime(&tp); 将秒数转为可以阅读的字符串
作者:
x2
时间:
2006-04-01 15:52
可以用 localtime 函数分别获取年月日时分秒的数值。
#include <time.h>
#include <stdio.h>
int main()
{
time_t now;
now = time(0);
tm *tnow = localtime(&now);
printf("%d %d %d %d %d %d\n", 1900+tnow->tm_year,
tnow->tm_mon+1,
tnow->tm_mday,
tnow->tm_hour,
tnow->tm_min,
tnow->tm_sec);
}
复制代码
[
本帖最后由 x2 于 2006-4-1 16:32 编辑
]
作者:
x2
时间:
2006-04-01 16:17
参考资料:
http://fanqiang.chinaunix.net/a4/b8/20010527/201001267.html
作者:
adm1ner
时间:
2006-04-01 17:17
lz的名字好强
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2