stdin,stdout 和 STDOUT_FILENO,STDIN_FILENO
stdin,stdout 等类型为 FILE *。
STDIN_FILENO,STDOUT_FILENO,STDERR_FILENO 等类型为 int。
使用 FILE * 的函数主要有:fopen、fread、fwrite、fclose等,基本上都以 f 开头。
使用 STDIN_FILENO 等的函数有:open、read、write、close等。
stdin 等属于标准 I/O,高级的输入输出函数,定义在 <stdio.h>。
STDIN_FILENO 等是文件描述符,是非负整数,一般定义为0, 1, 2,直接调用系统调用,定义在 <unistd.h>。
fileno() 函数可以用来取得 stream 指定的文件流所使用的文件描述符:
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("%d \n",fileno(stdin)); //0
printf("%d \n",fileno(stdout)); //1
printf("%d \n",fileno(stderr)); //2
return 0;
}
接受标准输入,输出到标准输出:
#include <stdio.h>
#include <unistd.h>
#define SIZE 100
int main()
{
int n;
char buf[SIZE];
while(n = read(STDIN_FILENO,buf,SIZE))
{
if(n != write(STDOUT_FILENO,buf,n))
perror("write error");
}
if(n < 0)
perror("read error");
return 0;
}
stdin,stdout 和 STDOUT_FILENO,STDIN_FILENO的更多相关文章
- stdin stdout stderr - 标准 I/O 流
Fd #include <stdio.h> Fd extern FILE *stdin; Fd extern FILE *stdout; Fd extern FILE *stderr; D ...
- vbs脚本要求在cmd中输入输出用StdIn ,StdOut
Dim StdIn, StdOutSet StdIn = WScript.StdInSet StdOut = WScript.StdOut Do While Not StdIn.AtEndOfStre ...
- WorkerMan源码分析(resetStd方法,PHP中STDIN, STDOUT, STDERR的重定向)
WorkerMan中work.php中 resetStd 方法中代码如下 public static function resetStd() { if (!static::$daemonize || ...
- stdin stdout stderr 标准I/O流
Unix中一切皆文件,磁盘等设备在操作系统来看都是文件. 对文件进行操作时,需要打开这个文件,并获得文件描述符(file descriptor, fd) 而每个进程生来就有三个文件描述符绑定在它身上, ...
- shell基础知识之 stdin,stdout,stderr和文件描述符
stdin,stdout,stderr stdin=0 stdout=1 stderr=2 使用tee来传递内容,把stdout 作为stdin 传到下个命令 root@172-18-21-195:/ ...
- 以冒泡排序为例--malloc/free 重定向stdin stdout
esort.c 代码如下,可关注下mallloc/free,freopen重定向的用法,排序为每轮将最小的数放在最前面: #include<stdio.h> #include<mal ...
- [LINK]php的三种CLI常量:STDIN,STDOUT,STDERR
FROM : http://www.cnblogs.com/thinksasa/archive/2013/02/27/2935158.html PHP CLI(command line interfa ...
- 【转】stdin, stdout, stderr 以及重定向
详细见: http://my.oschina.net/qihh/blog/55308 stdin是标准输入文件,stdout是标准输出文件,stderr标准出错文件. 程序按如下方式使用这些文件: 标 ...
- 重定向stdin stdout stderr |
在Linux下,当一个用户进程被创建的时候,系统会自动为该进程创建三个数据 流,也就是题目中所提到的这三个.那么什么是数据流呢(stream)? 我们知道,一个程序要运行,需要有输入.输出,如果出错, ...
随机推荐
- apktool android studio 调试 smali code, 重新打包
虽然有些菜单的位置跟新版的Android Stuido 3.4 有些不同,但是能用. https://crosp.net/blog/software-development/mobile/androi ...
- 使用arthas 生成火焰图分析jvm
arthas 是阿里巴巴开源的强大的jvm 应该分析工具,以下是使用arthas 生成jvm 火焰图的一个学习 项目使用docker-compose 运行,对于生成的火焰图使用nginx 提供一个访问 ...
- 11/9 <Stack> 155 232 225
155. Min Stack class MinStack { int min = Integer.MAX_VALUE; Stack<Integer> stack = new Stack& ...
- 【LG5330】[SNOI2019]数论
[LG5330][SNOI2019]数论 题面 洛谷 题目大意: 给定集合\(\mathbb {A,B}\) 问有多少个小于\(T\)的非负整数\(x\)满足:\(x\)除以\(P\)的余数属于\(\ ...
- [LeetCode] 753. Cracking the Safe 破解密码
There is a box protected by a password. The password is n digits, where each letter can be one of th ...
- Django初见
什么市WEB应用? 所谓的web应用就是我们基于浏览器打开的一个个网页(对应网址得到的内容) 软件开发架构 C/S架构: 客户端/服务端 B/S架构:浏览器/服务器 所有的B/S架构本质上就是C/S架 ...
- (十四)golang--函数和包
1.怎么定义函数? func (形参列表) 返回值列表{ 执行操作 return } 2.什么是包? 包的本质就是一个文件夹,存放程序文件 三大作用: 区分相同的名字的函数.变量等标识符: 当程序文件 ...
- Python学习教程(一)自学资源分享
Python 可以用来做什么? 在我看来,基本上可以不负责任地认为,Python 可以做任何事情.无论是从入门级选手到专业级选手都在做的爬虫,还是Web 程序开发.桌面程序开发还是科学计算.图像处理, ...
- 明解C语言 入门篇 第七章答案
练习7-1 #include <stdio.h> int main() { int n; printf(,, ); //此行显示结果为 4 3 6 因为1的字节就是为4,而-1的字节也是4 ...
- HTML+css基础 表格标签table Table标签属性 td标签属性
表格标签table: 他是由行与列构成,最小单位是单元格. 行标签 <tr></tr> 单元格标签<td></td> Table标签属性: Bor ...