【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下面提供了多种进程间通信的方法, 管道.信号.信号量.消息队列.共享内存.套接字等.下面我们分别 介绍管道.信号量.消息队列.共享内存. 信号和套 ...
随机推荐
- twobin博客样式—“蓝白之风”
自暑假以来,囫囵吞枣一般蒙头栽入前端自学中,且不说是否窥探其道,却不自觉中提高了对网页版面设计的要求,乃至挑剔.一个设计清爽美观的网页能让读者心旷神怡,甚至没有了阅读疲劳:而一个设计粗劣嘈杂的网页实在 ...
- Redis 主从配置和参数详解
安装redis 下载redis wget http://download.redis.io/releases/redis-3.0.7.tar.gz 解压redis tar -xvf redis-.ta ...
- Mint linux 自定义上下文菜单实现ZIP压缩文件无乱码解压
1. 前提条件 我的Mint Linux 是Thunar文件管理器(默认的). 2. 配置自定义动作 打开Thunar文件管理器,点击菜单“编辑”=>“配置自定义动作”.点击“+”添加一个新的. ...
- Android 解决方法数 65536 (65k) 限制
可能出现的错误信息: Conversion to Dalvik format failed: Unable to execute dex: method ID not in [0, 0xffff]: ...
- Android开发学习之路-带文字的图片分享
有用过微信分享SDK的都应该知道,微信分享到朋友圈的时候是不能同时分享图片和文字的,只要有缩略图,那么文字就不会生效.那么问题就来了,如果我们想把APP内的某些内容连带图片一起分享到微信,是不是没办法 ...
- 2017预防bug的重要性
Bug,中文名缺陷.一个让软件测试员兴奋,让开发人员头疼的词.来源二次大战期间,一个称为"马克二型"的计算机,由于天气过热,硬件跟不上导致死机.最后发现是因为飞蛾,被继电器电死,将 ...
- show master/slave status求根溯源
show master/slave status分别是查看主数据库以及副数据库的状态,是一种能查看主从复制运行情况的方式. 这里仅仅讨论linux下的nysql5.7.13版本的执行情况 一.show ...
- WPF MVVM框架下,VM界面写控件
MVVM正常就是在View页面写样式,ViewModel页面写逻辑,但是有的时候纯在View页面写样式并不能满足需求.我最近的这个项目就遇到了,因此只能在VM页面去写样式控件,然后绑定到View页面. ...
- 用jquery.pager.js实现分页
1.html <link href="/stylesheets/Pager.css" rel="stylesheet" type="text/c ...
- lintcode 落单的数(位操作)
题目1 落单的数 给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字. 链接:http://www.lintcode.com/zh-cn/problem/single ...