【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下面提供了多种进程间通信的方法, 管道.信号.信号量.消息队列.共享内存.套接字等.下面我们分别 介绍管道.信号量.消息队列.共享内存. 信号和套 ...
随机推荐
- Azure PowerShell (9) 使用PowerShell导出订阅下所有的Azure VM的Public IP和Private IP
<Windows Azure Platform 系列文章目录> 笔者在之前的工作中,有客户提出想一次性查看Azure订阅下的所有Azure VM的Public IP和Private IP. ...
- Android开发学习之路-Android6.0运行时权限
在Android6.0以后开始,对于部分敏感的“危险”权限,需要在应用运行时向用户申请,只有用户允许的情况下这个权限才会被授予给应用.这对于用户来说,无疑是一个提升安全性的做法.那么对于开发者,应该怎 ...
- C语言-指针
C指针基础知识 C语言中,指针无疑是最令人头疼的.今天无事就来学学C语言的指针,在此留下点笔记,仅供个人参考. 首先要搞懂的是,指针是什么? 指针:是用来存放内存地址的变量. 不管是什么类型的指针,存 ...
- C#学习系列-文章导航
C#学习系列-.NET体系结构 C#学习系列-类与结构的区别 C#学习系列-String与string的区别 C#学习系列-抽象方法与虚拟方法的区别 C#学习系列-out与ref的区别 C#学习系列- ...
- 虚拟机网络驱动(共享文件夹)不见了的解决方案-适用于win7~win10 and Windows Server 2008~Windows Server 2012R2
具体看图 手动打入下面选择部分的字符 \\vmware-host\Shared Folders 然后就可以了,这边有个红叉,重启后就没了 重启后
- Entity Framework Code First关系映射约定
本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...
- 实战MEF(5):导出元数据
如何理解元数 我们可以把元数据理解为随类型一起导出的附加信息.有时候我们会考虑,把元数据随类型一并导出,增加一些说明,使得我们在导入的时候,可以多一些筛选条件. 默认的类型导出带有元数据吗 上面的内容 ...
- iOS开发之使用Storyboard预览UI在不同屏幕上的运行效果
在公司做项目一直使用Storyboard,虽然有时会遇到团队合作的Storyboard冲突问题,但是对于Storyboard开发效率之高还是比较划算的.在之前的博客中也提到过,团队合作使用Storyb ...
- Percona博客学习总结
1. Upgrading to MySQL 5.7, focusing on temporal types 在MySQL 5.6.4中,对TIME, TIMESTAMP and DATETIME三种时 ...
- C算法编程题(二)正螺旋
前言 上一篇<C算法编程题(一)扑克牌发牌> 写东西前总是喜欢吐槽一些东西,还是多啰嗦几句吧,早上看了一篇博文<谈谈外企涨工资那些事>,里面楼主讲到外企公司包含的五类人,其实不 ...