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的更多相关文章

  1. stdin stdout stderr - 标准 I/O 流

    Fd #include <stdio.h> Fd extern FILE *stdin; Fd extern FILE *stdout; Fd extern FILE *stderr; D ...

  2. vbs脚本要求在cmd中输入输出用StdIn ,StdOut

    Dim StdIn, StdOutSet StdIn = WScript.StdInSet StdOut = WScript.StdOut Do While Not StdIn.AtEndOfStre ...

  3. WorkerMan源码分析(resetStd方法,PHP中STDIN, STDOUT, STDERR的重定向)

    WorkerMan中work.php中 resetStd 方法中代码如下 public static function resetStd() { if (!static::$daemonize || ...

  4. stdin stdout stderr 标准I/O流

    Unix中一切皆文件,磁盘等设备在操作系统来看都是文件. 对文件进行操作时,需要打开这个文件,并获得文件描述符(file descriptor, fd) 而每个进程生来就有三个文件描述符绑定在它身上, ...

  5. shell基础知识之 stdin,stdout,stderr和文件描述符

    stdin,stdout,stderr stdin=0 stdout=1 stderr=2 使用tee来传递内容,把stdout 作为stdin 传到下个命令 root@172-18-21-195:/ ...

  6. 以冒泡排序为例--malloc/free 重定向stdin stdout

    esort.c 代码如下,可关注下mallloc/free,freopen重定向的用法,排序为每轮将最小的数放在最前面: #include<stdio.h> #include<mal ...

  7. [LINK]php的三种CLI常量:STDIN,STDOUT,STDERR

    FROM : http://www.cnblogs.com/thinksasa/archive/2013/02/27/2935158.html PHP CLI(command line interfa ...

  8. 【转】stdin, stdout, stderr 以及重定向

    详细见: http://my.oschina.net/qihh/blog/55308 stdin是标准输入文件,stdout是标准输出文件,stderr标准出错文件. 程序按如下方式使用这些文件: 标 ...

  9. 重定向stdin stdout stderr |

    在Linux下,当一个用户进程被创建的时候,系统会自动为该进程创建三个数据 流,也就是题目中所提到的这三个.那么什么是数据流呢(stream)? 我们知道,一个程序要运行,需要有输入.输出,如果出错, ...

随机推荐

  1. IP、MAC和端口号(六)

    在茫茫的互联网海洋中,要找到一台计算机非常不容易,有三个要素必须具备,它们分别是 IP 地址.MAC 地址和端口号. 一.IP地址 IP地址是 Internet Protocol Address 的缩 ...

  2. java 启动jar 指定端口

    java 启动jar 指定端口 java -jar xxx.jar --server.port=80

  3. VIJOS-P1059 积木城堡

    洛谷 P1504 积木城堡 https://www.luogu.org/problem/P1504 JDOJ 1240: VIJOS-P1059 积木城堡 https://neooj.com/oldo ...

  4. KDE 上 任务栏 图标位置更改

    任务栏上图标是不能直接拖拽的. 右键点任务栏,选[Panel Options -> Panel Settins]之后,就可以拖拽了. 完成之后,按X就行了.

  5. MySQL字段类型 约束

    目录 MySQL存储引擎 非空约束 字段类型 整形类型INT TINYINT 浮点类型float 字符类型char varchar 日期类型 枚举集合 约束条件 主键 自增 unsigned无符号 z ...

  6. Manthan Codefest 19 题解

    这套题还是有点质量的吧 -- 题目链接 A. XORinacci 傻叉签到题,因为异或的性质所以这个序列的循环节长度只有 \(3\) -- 查看代码 B. Uniqueness 因为序列长度乃至数的种 ...

  7. 文本特征提取---词袋模型,TF-IDF模型,N-gram模型(Text Feature Extraction Bag of Words TF-IDF N-gram )

    假设有一段文本:"I have a cat, his name is Huzihu. Huzihu is really cute and friendly. We are good frie ...

  8. [LeetCode] 272. Closest Binary Search Tree Value II 最近的二分搜索树的值之二

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  9. [LeetCode] 69. Sqrt(x) 求平方根

    Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...

  10. Ubuntu 14.04 apt-get update失效解决(转)

    现象如下: VirtualBox:~$ sudo apt-get update Err http://mirrors.aliyun.com trusty InRelease Err http://mi ...