【linux草鞋应用编程系列】_6_ 重定向和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() Linux Programmer’s Manual DUP()
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[], O_RDWR|O_CREAT, );
if(- == fd )
{
perror("open");
exit();
} fd=dup2(fd,); //将 fd 指向了 stdout , 即将 stdout 重定向到了 fd 描述的文件
if(- == fd)
{
perror("dup2");
exit();
} write(,"abc\n",sizeof("abc\n")); printf("this statement can not write to stdout.\n"); close(fd);
return ;
}
程序的执行过程如下:
[root@localhost redirectory]# ll
总计
-rwxr-xr-x root root - : a.out
-rw-r--r-- root root - : 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[], O_RDWR|O_CREAT, );
if(- == fd )
{
perror("open");
exit();
}
#if 0
fd=dup2(fd,);
if(- == fd)
{
perror("dup2");
exit();
}
#endif
write(,"abcdefg\n",sizeof("abcdefg\n")); printf("this statement can write to stdout.\n"); close(fd);
return ;
}
[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(); printf("the new fd is: %d\n",fd);
write(fd,"abcdefg\n",sizeof("abcdefg\n")); close(fd);
return ;
}
[root@localhost redirectory]# vim dup.c
[root@localhost redirectory]# gcc dup.c
[root@localhost redirectory]# ./a.out
the new fd is: //默认dup()成功返回最小的未分配的文件描述符
abcdefg //通过fd, 实现了对 stdout = 1 标准输出的写操作
[root@localhost redirectory]#
#include <stdio.h> int main(void)
{
printf("\033#8");
return ;
}
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 ;
}
#include <stdio.h> int main(void)
{
int i;
for(i=;i<;i++)
{
printf("\033[37m\033[40m");
printf("0123456789abcdefghijklmnopqrstuvwxyz\n");
printf("\033[30m\033[47m");
printf("0123456789abcdefghijklmnopqrstuvwxyz\n");
}
return ;
}

TIME() Linux Programmer’s Manual TIME()
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 ;
}
[root@localhost time]# ./a.out
From - :: have past secnods:
CTIME() Linux Programmer’s Manual CTIME()
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[]; time(&t);
p_tmp=localtime_r(&t,&tm);
if(NULL == p_tmp)
{
perror("get local time");
exit();
} sprintf(buf,"%d-%d-%d %d %d-%d-%d", + tm.tm_year, + tm.tm_mon,tm.tm_mday,
tm.tm_wday,
tm.tm_hour,tm.tm_min,tm.tm_sec); printf("%s\n",buf); return ;
}
[root@localhost time]# ./a.out
-- -- //2013年11月12日 星期4 16:30:04
STRFTIME() Linux Programmer’s Manual STRFTIME()
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[]; //clear screen
printf("\033[2J");
for(;;)
{
time(&t);
p_tmp=localtime_r(&t,&tm);
if(NULL == p_tmp)
{
perror("get local time");
exit();
} strftime(buf,,"%Y-%m-%d %A %T %z",&tm); printf("\033[H\033[34m");
printf("%s\n",buf);
sleep();
}
return ;
}
[root@localhost input]# ls
event0 event1 event2 event3 event4 js0 mice mouse0 mouse1 mouse2
[root@localhost input]#
【linux草鞋应用编程系列】_6_ 重定向和VT100编程的更多相关文章
- 重定向和VT100编程
重定向和VT100编程 一.文件重定向 我们知道在linux shell 编程的时候,可以使用文件重定向功能,如下所示: [root@localhost pipe]# echo "h ...
- [C# 网络编程系列]专题八:P2P编程
引言: 前面的介绍专题中有朋友向我留言说介绍下关于P2P相关的内容的,首先本人对于C#网络编程也不是什么大牛,因为能力的关系,也只能把自己的一些学习过程和自己的一些学习过程中的理解和大家分享下的,下面 ...
- [C# 网络编程系列]专题七:UDP编程补充——UDP广播程序的实现
转自:http://www.cnblogs.com/zhili/archive/2012/09/03/2666974.html 上次因为时间的关系,所以把上一个专题遗留下的一个问题在本专题中和大家分享 ...
- [C# 网络编程系列]专题六:UDP编程
转自:http://www.cnblogs.com/zhili/archive/2012/09/01/2659167.html 引用: 前一个专题简单介绍了TCP编程的一些知识,UDP与TCP地位相当 ...
- [C# 网络编程系列]专题五:TCP编程
转自:http://www.cnblogs.com/zhili/archive/2012/08/25/2656840.html 前言 前面专题的例子都是基于应用层上的HTTP协议的介绍, 现在本专题来 ...
- 【linux草鞋应用编程系列】_1_ 开篇_系统调用IO接口与标准IO接口
最近学习linux系统下的应用编程,参考书籍是那本称为神书的<Unix环境高级编程>,个人感觉神书不是写给草鞋看的,而是 写给大神看的,如果没有一定的基础那么看这本书可能会感到有些头重脚轻 ...
- 【linux草鞋应用编程系列】_5_ Linux网络编程
一.网络通信简介 第一部分内容,暂时没法描述,内容实在太多,待后续专门的系列文章. 二.linux网络通信 在linux中继承了Unix下“一切皆文件”的思想, 在linux中要实现网 ...
- 【linux草鞋应用编程系列】_4_ 应用程序多线程
一.应用程序多线程 当一个计算机上具有多个CPU核心的时候,每个CPU核心都可以执行代码,此时如果使用单线程,那么这个线程只能在一个 CPU上运行,那么其他的CPU核心就处于空闲状态,浪费了系 ...
- 【linux草鞋应用编程系列】_3_ 进程间通信
一.进程间通信 linux下面提供了多种进程间通信的方法, 管道.信号.信号量.消息队列.共享内存.套接字等.下面我们分别 介绍管道.信号量.消息队列.共享内存. 信号和套 ...
随机推荐
- js中几种实用的跨域方法原理详解
这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...
- 如何给CentOS安装字体库
很多时候,我们需要做一些图像生成工作(譬如验证码之类的),这时候,我们一般都需要用到系统的字体库.但事情却总非尽善人意,我们所使用的Linux操作系统无法像Windows操作系统那样足够“旗舰”,字体 ...
- Objective-C 桥接模式 -- 简单实用和说明
桥接模式---把两个相关联的类抽象出来, 以达到解耦的目的 比如XBox遥控器跟XBox主机, 我们抽象出主机和遥控器两个抽象类, 让这两个抽象类耦合 然后生成这两个抽象类的实例XBox & ...
- sublime3 插件
Sublime Text 3能用支持的插件推荐 从二月份用测试版本build 3012开始用sublime text 3,虽然很多插件在sublime text 3不工作了,因为sublime tex ...
- Spring学习记录(五)---bean的作用域scope
作用域:singleton:单例,整个应用中只创建一个实例(默认) prototype:原型,每次注入时都新建一个实例 session:会话,每个会话创建一个实例 request:请求,每个请求创建一 ...
- 谈谈service层在mvc框架中的意义和职责
mvc框架由model,view,controller组成,执行流程一般是:在controller访问model获取数据,通过view渲染页面. mvc模式是web开发中的基础模式,采用的是分层设计, ...
- EF-DbUpdateException--实体类和数据库列不对应的解决方案
错误信息 1.VS实体类里面的字段 2数据库里面的字段 猜测是因为字段数不匹配导致的 3删除多余字段 5.结果 错误信息贴上: -------------------------Log_Header- ...
- 【开源】OSharp框架解说系列(3):扩展方法
OSharp是什么? OSharp是个快速开发框架,但不是一个大而全的包罗万象的框架,严格的说,OSharp中什么都没有实现.与其他大而全的框架最大的不同点,就是OSharp只做抽象封装,不做实现.依 ...
- 感恩回馈,新鲜出炉的《ASP.NET MVC 5框架揭秘》免费赠送
上次针对<ASP.NET Web API 2框架揭秘>举办了一次评论赠书活动,很多人问我相同的活动要不要针对<ASP.NET MVC 5框架揭秘>(阅读样章)再来一次,为此我向 ...
- 借助node实战JSONP跨域
一.前言: 浏览器安全是基于同源策略的.所谓同源策略就是三相同: 1.协议相同: 2.域名相同: 3.端口相同. 但,凡事都是有利弊,同源策略也导致了我们想用AJAX跨域请求,但NO!!为了规避这种限 ...