重定向和VT100编程
一、文件重定向
[root@localhost pipe]# echo "hello world"
hello world //没有进行重定向,在终端显示
[root@localhost pipe]# echo "hello world" > txt //进行重定向,不在终端显示
[root@localhost pipe]# cat txt //查看生成的文件 txt 的内容
hello world
[root@localhost pipe]#

DUP(2) Linux Programmer’s Manual DUP(2)
NAME
dup, dup2 - duplicate a file descriptor //复制文件描述符
SYNOPSIS
#include <unistd.h> int dup(int oldfd); //要被替代掉的文件描述符, 旧文件描述符 int dup2(int oldfd, //要被替代掉的文件描述符, 旧文件描述符
int newfd); //替代的文件描述符, 新文件描述符


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h> int main(int argc,char* argv[])
{
int fd; fd=open(argv[1], O_RDWR|O_CREAT, 0666);
if(-1 == fd )
{
perror("open");
exit(1);
} fd=dup2(fd,1); //将 fd 指向了 stdout , 即将 stdout 重定向到了 fd 描述的文件
if(-1 == fd)
{
perror("dup2");
exit(1);
} write(1,"abc\n",sizeof("abc\n")); printf("this statement can not write to stdout.\n"); close(fd);
return 0;
}

程序的执行过程如下:

[root@localhost redirectory]# ll
总计 12
-rwxr-xr-x 1 root root 5436 12-12 14:18 a.out
-rw-r--r-- 1 root root 468 12-12 14:18 main.c
[root@localhost redirectory]# ./a.out txt //可以看到 printf函数没有输出,而通过 write(1,xx,xx) 的信息也没有输出到stdout
[root@localhost redirectory]# cat txt //数据写入到了txt, 查看txt 的内容
abc
[root@localhost redirectory]#


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h> int main(int argc,char* argv[])
{
int fd; fd=open(argv[1], O_RDWR|O_CREAT, 0666);
if(-1 == fd )
{
perror("open");
exit(1);
}
#if 0
fd=dup2(fd,1);
if(-1 == fd)
{
perror("dup2");
exit(1);
}
#endif
write(1,"abcdefg\n",sizeof("abcdefg\n")); printf("this statement can write to stdout.\n"); close(fd);
return 0;
}


[root@localhost redirectory]# vim main.c
[root@localhost redirectory]# gcc main.c
[root@localhost redirectory]# cat txt
abc
[root@localhost redirectory]# ./a.out txt
abcdefg //write(1,xx,xx) 正常从标准输出输出
this statement can write to stdout. //printf 也正常输出到标准输出
[root@localhost redirectory]# cat txt
abc
[root@localhost redirectory]#


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h> int main(int argc,char* argv[])
{
int fd; fd=dup(1); printf("the new fd is: %d\n",fd);
write(fd,"abcdefg\n",sizeof("abcdefg\n")); close(fd);
return 0;
}

[root@localhost redirectory]# vim dup.c
[root@localhost redirectory]# gcc dup.c
[root@localhost redirectory]# ./a.out
the new fd is: 3 //默认dup()成功返回最小的未分配的文件描述符
abcdefg //通过fd, 实现了对 stdout = 1 标准输出的写操作
[root@localhost redirectory]#

#include <stdio.h> int main(void)
{
printf("\033#8");
return 0;
}

EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
[root@localhost vt100]# EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
[root@localhost vt100]# echo -ne "\033#8"

#include <stdio.h> int main(void)
{
for(;;)
{
printf("\033[H");
printf("0123456789abcdefghijklmnopqrstuvwxyz");
}
return 0;
}


#include <stdio.h> int main(void)
{
int i;
for(i=0;i<3;i++)
{
printf("\033[37m\033[40m");
printf("0123456789abcdefghijklmnopqrstuvwxyz\n");
printf("\033[30m\033[47m");
printf("0123456789abcdefghijklmnopqrstuvwxyz\n");
}
return 0;
}



TIME(2) Linux Programmer’s Manual TIME(2)
NAME
time - get time in seconds //获取从1970年1月1日0时0分0秒 到 当前时刻的 总秒数
SYNOPSIS
#include <time.h> time_t time(time_t *t); //输出参数,存储获取的秒


#include <stdio.h>
#include <time.h> int main(void)
{
time_t t; printf("From 1970 01-01 0:00:00 have past secnods:%ld\n", time(&t)); return 0;
}

[root@localhost time]# ./a.out
From 1970 01-01 0:00:00 have past secnods:1386835671

CTIME(3) Linux Programmer’s Manual CTIME(3)
NAME
asctime, ctime, gmtime, localtime, mktime, asctime_r, ctime_r, gmtime_r, local-
time_r - transform date and time to broken-down time or ASCII
SYNOPSIS
#include <time.h> char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf); char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf); struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result); struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result); time_t mktime(struct tm *tm);


struct tm {
int tm_sec; /* seconds */ //秒
int tm_min; /* minutes */ //分
int tm_hour; /* hours */ //时
int tm_mday; /* day of the month */ //一个月的第几天, 即 日
int tm_mon; /* month */ //月
int tm_year; /* year */ //年, 从1900年到现在的年数
int tm_wday; /* day of the week */ //星期几
int tm_yday; /* day in the year */ //一年中的第几天
int tm_isdst; /* daylight saving time */
};


#include <stdio.h>
#include <time.h>
#include <stdlib.h> int main(void)
{
time_t t;
struct tm tm;
void* p_tmp;
char buf[32]; time(&t);
p_tmp=localtime_r(&t,&tm);
if(NULL == p_tmp)
{
perror("get local time");
exit(1);
} sprintf(buf,"%d-%d-%d %d %d-%d-%d", 1900 + tm.tm_year, 1+ tm.tm_mon,tm.tm_mday,
tm.tm_wday,
tm.tm_hour,tm.tm_min,tm.tm_sec); printf("%s\n",buf); return 0;
}

[root@localhost time]# ./a.out
2013-12-12 4 16-30-4 //2013年11月12日 星期4 16:30:04

STRFTIME(3) Linux Programmer’s Manual STRFTIME(3)
NAME
strftime - format date and time SYNOPSIS
#include <time.h> size_t strftime(char *s, //存储格式化后的字符串
size_t max, //字符串的最大长度
const char *format, //格式化字符串
const struct tm *tm); //struct time 的结构体指针


#include <stdio.h>
#include <time.h>
#include <stdlib.h> int main(void)
{
time_t t;
struct tm tm;
void* p_tmp;
char buf[64]; //clear screen
printf("\033[2J");
for(;;)
{
time(&t);
p_tmp=localtime_r(&t,&tm);
if(NULL == p_tmp)
{
perror("get local time");
exit(1);
} strftime(buf,64,"%Y-%m-%d %A %T %z",&tm); printf("\033[H\033[34m");
printf("%s\n",buf);
sleep(1);
}
return 0;
}

[root@localhost input]# ls
event0 event1 event2 event3 event4 js0 mice mouse0 mouse1 mouse2
[root@localhost input]#
重定向和VT100编程的更多相关文章
- 【linux草鞋应用编程系列】_6_ 重定向和VT100编程
一.文件重定向 我们知道在linux shell 编程的时候,可以使用文件重定向功能,如下所示: [root@localhost pipe]# echo "hello world&q ...
- 详解Win2003 IIS6.0 301重定向带参数的问题(转摘)
网站更换域名,把旧域名用301指到新域名来. 从iis中设置url永久转向就可以,看上去很容易,用了一会儿才发现,参数都没有带上. 从微软网站上找到如下说明,果然好使: 重定向参考 (IIS 6. ...
- Intel 80x86 Linux Kernel Interrupt(中断)、Interrupt Priority、Interrupt nesting、Prohibit Things Whthin CPU In The Interrupt Off State
目录 . 引言 . Linux 中断的概念 . 中断处理流程 . Linux 中断相关的源代码分析 . Linux 硬件中断 . Linux 软中断 . 中断优先级 . CPU在关中断状态下编程要注意 ...
- 【转载】应读者强烈要求给出《超容易的Linux系统管理入门书》一书的主要知识点
刚开始了一篇连载,收到广大Linux爱好者的反馈,非常欣慰.大家对Linux学习感到很迷茫,不知道学哪些内容,如何学习? <超容易的Linux系统管理入门书>一书是腾讯Linux专家在腾讯 ...
- Webfrom基础知识
MyBeNASP.NET内置对象 (1)简述ASP.NET内置对象. 答:ASP.NET提供了内置对象有Page.Request.Response.Application.Session.Server ...
- 如何利用 LTE/4G 伪基站+GSM 中间人攻击攻破所有短信验证
这次公开课请来的嘉宾对自己的简介是: 连续创业失败的创业导师:伪天使投资人:某非知名私立大学创办人兼校长:业余时间在本校通信安全实验室打杂. 自从他在黑客大会上演讲<伪基站高级利用技术——彻底攻 ...
- webConfig详细跳转配置.[转]
站更换域名,把旧域名用301指到新域名来. 从iis中设置url永久转向就可以,看上去很容易,用了一会儿才发现,参数都没有带上. 从微软网站上找到如下说明,果然好使: 重定向参考 (IIS 6.0,7 ...
- 2019-2020-1 20199302《Linux内核原理与分析》第八周作业
一.上课学习笔记 1.shell作用:①运行程序 ②重定向(输入/输出重定向) ③可编程(写脚本) 执行一个c程序时,如果切进另一个进程,会进入该进程而切不回原进程,所以需要为调用的进程创一个子进程. ...
- 请求转发:MVC设计模式、细节、请求域属性的编程实例、请求重定向和请求转发的区别
请求转发:MVC设计模式.细节.请求域属性的编程实例.请求重定向和请求转发的区别 MVC设计模式将一次请求的响应过程分成三个功能模块(一般称之为层)来协同完成,这三个模块分别是Model(模型层) ...
随机推荐
- VAXVOIP SDK Licensekey
Insert the following key to the SDK with SetLicenseKey call: VAXVOIP.COM-191P238P55P253P97P229P51P76 ...
- rdlc报告vs2008编辑正常,在vs2012在对错误的编辑
最近我们的系统开发的工具vs2008升级到2012,由于系统是非常的报告是由rdlc发展.今天 有需要修改的报告满足需求.直接使用vs2012正确rdlc报告编辑,结果本次变动后.报表都报错. 后来我 ...
- .net设计模式之装饰模式
概述: 装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 装饰模式的特点: (1) 装饰对象和真实对象有相同的接口.这样 ...
- 清理收缩VMware虚拟机MacOS系统的vmdk文件大小
屌丝行和差的主要标准,尽管持续性眼贪婪mbp.但是,从另一方面限制患有米,只是在虚拟机中播放MacOS.(我不会告诉你我的笔记本i5+120SSD+500HHD+12G内存,跑MacOS虚拟机一点不卡 ...
- 信号量机制DOWN操作和UP操作的详细说明
DOWN操作:linux内核.信号DOWN例如,下面的操作: void down(struct semaphore *sem); //不间断 int down_interruptible(struct ...
- 【高德地图API】从零开始学高德JS API(二)地图控件与插件——测距、圆形编辑器、鼠标工具、地图类型切换、鹰眼鱼骨
原文:[高德地图API]从零开始学高德JS API(二)地图控件与插件——测距.圆形编辑器.鼠标工具.地图类型切换.鹰眼鱼骨 摘要:无论是控件还是插件,都是在一级API接口的基础上,进行二次开发,封装 ...
- Codeforces 328B-Sheldon and Ice Pieces(馋)
B. Sheldon and Ice Pieces time limit per test 1 second memory limit per test 256 megabytes input sta ...
- 赵雅智_Android编码规范
凝视 导入mycodetemplates.xml统一凝视样式 须要加凝视的地方 类凝视(必加) 方法凝视(必加) 块凝视主要是数据结构和算法的描写叙述(必加) 类成员变量和常量凝视(选择性加入) 单行 ...
- 数据传输对象(DTO)介绍及各类型实体比较
数据传输对象(DTO)介绍及各类型实体比较 本文将介绍DDD分层架构中广泛使用的数据传输对象Dto,并且与领域实体Entity,查询实体QueryObject,视图实体ViewModel等几种实体进行 ...
- Xcode下执行HelloWorld
准备工作 到Cocos2d-x官方站点下载最新版本号v3.0beta2 创建HelloWorld项目 将刚才下载的压缩包解压到你指定的目录里. 进入到文件夹**cocos2d-x-3.0beta2/t ...