重定向
freopen(“test.txt”,“a”,stdout);
puts(“puts to stdout”);
终端不显示,puts的内容被重定向追加到test.txt中
把参数3重定向到参数1
stdin 键盘
stdout 显示器
stderror 错误
fflush(stdout) ; 刷新缓冲区
int stdout_1=dup(fileno(stdout));
dup2(stdout_1,(fileno(stdout)));//恢复控制台输出
close(stdout_1);
#include <stdio.h>
int main()
{
int stdout_1=dup(fileno(stdout));
printf("龙战于野\n");
freopen("test.txt","w",stdout);//把显示屏的输入转移到文本中
printf("123 龙战于野\n");
freopen("test.txt","r",stdin);
int a;
scanf("%d",&a);
printf("%d\n",a);
fflush(stdout);
dup2(stdout_1,(fileno(stdout)));
printf("%d\n",a);
close(stdout_1);
return 0;
}
时间
#include <sys/times.h>
#include <time.h>
#include <utime.h>
#include <stdlib.h>
// #include <sys/times.h>
#include <stdio.h>
#include <time.h>
#include <utime.h>
#include <stdlib.h>
int main()
{
//1970.1.1 0时0分0秒至今
time_t t1;
time_t ret;
ret=time(&t1);
//time()函数返回值就是时间戳,实参是地址,地址里面的内容也是时间戳
printf("%lld\n",ret);
printf("%lld\n",t1);
struct timespec s1;
clock_gettime(CLOCK_REALTIME,&s1);
long long ms=s1.tv_sec*1000+s1.tv_nsec/1000000;
printf("%lld\n",ms);
long long us=s1.tv_sec*1000*1000+s1.tv_nsec/1000;
printf("%lld\n",us);
return 0;
}
:
1660202685
1660202685
1660202685399
1660202685399838
--------------------------------
Process exited after 0.1759 seconds with return value 0
请按任意键继续. . .
.
#include <stdio.h>
#include <time.h>
#include <utime.h>
#include <stdlib.h>
int main()
{
time_t t1;
time(&t1);
struct tm *timeData=localtime(&t1);
printf("%d\n",t1);
printf("tm_sec=%d\n",timeData->tm_sec);
printf("tm_min=%d\n",timeData->tm_min);
printf("tm_hour=%d\n",timeData->tm_hour);
printf("tm_mday=%d\n",timeData->tm_mday);
printf("tm_mon=%d\n",timeData->tm_mon+1);//0-11
printf("tm_year=%d\n",timeData->tm_year+1900);//距离1900
printf("tm_wday=%d\n",timeData->tm_wday);//周几
printf("tm_yday=%d\n",timeData->tm_yday);//今年第多少天
printf("tm_isdst=%d\n",timeData->tm_isdst);
//日历转换为时间戳
time_t t2=mktime(timeData);
printf("%d\n",t2);
//获取格林威治时间
struct tm *gmData=gmtime(&t2);
printf("%d:%d:%d\n",gmData->tm_hour,gmData->tm_min,gmData->tm_sec);
return 0;
}
:
1660202720
tm_sec=20
tm_min=25
tm_hour=15
tm_mday=11
tm_mon=8
tm_year=2022
tm_wday=4
tm_yday=222
tm_isdst=0
1660202720
7:25:20
--------------------------------
Process exited after 0.2046 seconds with return value 0
请按任意键继续. . .
.
#include <stdio.h>
int main()
{
time_t t1;
time(&t1);
struct tm * tdate;
tdate=localtime(&t1);//将时间戳转换为日历
puts(asctime(tdate));//输出
puts(ctime(&t1));//输出
char str_time[30];
size_t ret=strftime(str_time,20,"%Y-%m-%d %H:%M:%S",tdate);
//参数1缓冲区 参数2大小 参数3格式转换符 参数4存储时间的变量
printf("ret=%d\n",ret);
puts(str_time);
return 0;
}
:
Thu Aug 11 15:25:54 2022
Thu Aug 11 15:25:54 2022
ret=19
2022-08-11 15:25:54
--------------------------------
Process exited after 0.1839 seconds with return value 0
请按任意键继续. . .