`

C语言中标准输入流、标准输出流、标准错误输出流

阅读更多

C语言中标准输入流、标准输出流、标准错误输出流


在Linux中,所有对设备和文件的操作都使用文件描述符来进行。

Linux中一个进程启动时,都会打开3个文件:标准输入、标准输出和标准出错处理。这三个文件分别对应文件描述符0、1、2。

 

在C语言中,在程序开始运行时,系统自动打开3个标准文件:标准输入、 标准输出、标准出错输出。通常这3个文件都与终端相联系。因此,以前我们所用到的从终端输入或输出都不需要打开终端文件。系统自定义了3个文件指针stdin、stdout、stderr,分别指向终端输入、终端输出和标准出错输出(也从终端输出)。

标准输入流:stdin

标准输出流:stdout

标准错误输出流:stderr

 


测试代码:

#include <stdio.h>

int main()
{
        int a = 0;
        char szTmp[100] = {0};
        try
        {
                fprintf(stdout, "please input a:\n");
                fscanf(stdin, "%d", &a);
                fprintf(stdout, "a=%d\n", a);

                fprintf(stdout, "please input szTmp:\n");
                fscanf(stdin, "%s", szTmp);
                fprintf(stdout, "szTmp:%s\n", szTmp);
        }
        catch(...)
        {
                fprintf(stderr, "input error!");
        }

        return 0;
}
 



编译:

g++ -o a a.cpp


测试结果:

./a 

please input a:

123

a=123

please input szTmp:

aaaaaaaaaaaaaaaaaaaaaaa

szTmp:aaaaaaaaaaaaaaaaaaaaaaa



PS:相关文档

stdin


object

<cstdio>

FILE * stdin;

Standard input stream

The standard input stream is the default source of data for applications. It is usually directed to the input device of the standard console (generally, a keyboard).


stdin can be used as an argument for any function that expects an input stream as one of its parameters, like fgets or fscanf.


Although it is generally safe to assume that the source of data for stdin is going to be a keyboard, bear in mind that this may not be the case even in regular console systems, since stdin can be redirected at the operating system level. For example, many systems, among them DOS/Windows and most UNIX shells, support the following command syntax:


myapplication < example.txt


to use the content of the file example.txt as the primary source of data for myapplication instead of the console keyboard.


It is also possible to redirect stdin to some other source of data from within a program using the freopen function.

stdout



object

<cstdio>

FILE * stdout;

Standard output stream

The standard output stream is the default destination of regular output for applications. It is usually directed to the output device of the standard console (generally, the screen).


stdout can be used as an argument for any function that expects an output stream as one of its parameters, like fputs or fprintf.


Although it is generally safe to assume that the default destination for stdout is going to be the screen, bear in mind that this may not be the case even in regular console systems, since stdout can be redirected at the operating system level. For example, many systems, among them DOS/Windows and most UNIX shells, support the following command syntax:


myapplication > example.txt


to redirect the output of myapplication to the file example.txt instead of the screen.


It is also possible to redirect stdout to some other source of data from within a program using the freopen function.

stderr



object

<cstdio>

FILE * stderr;

Standard error stream

The standard error stream is the default destination for error messages and other diagnostic warnings. Like stdout, it is usually also directed to the output device of the standard console (generally, the screen).


stderr can be used as an argument for any function that expects an output stream as one of its parameters, like fputs or fprintf.


Although generally both stdout and stderr are associated with the same console output, applications may differentiate between what is sent to stdout and what to stderrfor the case that one of them is redirected. For example, it is frequent to redirect the regular output of a console program (stdout) to a file while expecting the error messages to keep appearing in the console screen.


It is also possible to redirect stderr to some other destination from within a program using the freopen function.

perror



function

<cstdio>

void perror ( const char * str );

Print error message

Interprets the value of the global variable errno into a string and prints that string to stderr (standard error output stream, usually the screen), optionaly preceding it with the custom message specified in str.

errno is an integral variable whose value describes the last error produced by a call to a library function. The error strings produced by perror depend on the developing platform and compiler.

If the parameter str is not a null pointer, str is printed followed by a colon (:) and a space. Then, whether str was a null pointer or not, the generated error description is printed followed by a newline character ('\n').

perror should be called right after the error was produced, otherwise it can be overwritten in calls to other functions.


Parameters.


str

C string containing a custom message to be printed before the error message itself.

If it is a null pointer, no preceding custom message is printed, but the error message is printed anyway.

By convention, the name of the application itself is generally used as parameter.


 

分享到:
评论

相关推荐

    C语言程序设计标准教程

    [例10.3]把命令行参数中的前一个文件名标识的文件, 复制到后一个文件名标识的文件中, 如命令行中只有一个文件名则把该文件写到标准输出文件(显示器)中。 #include main(int argc,char *argv[]) { FILE *fp1,*fp2; ...

    C语言标准教程第一章 C语言概论

    但是,在《K&R》中并没有定义一个完整的标准C语言,后来由美国国家标准学会在此基础上制定了一个C 语言标准,于一九八三年发表。通常称之为ANSI C。 当代最优秀的程序设计语言  早期的C语言主要是用于UNIX系统。...

    C语言入门经典(第4版)--源代码及课后练习答案

    10.2 标准流 370 10.3 键盘输入 371 10.3.1 格式化键盘输入 371 10.3.2 输入格式控制字符串 372 10.3.3 输入格式字符串中的字符 377 10.3.4 输入浮点数的各种变化 378 10.3.5 读取十六进制和八进制值 379 ...

    C++讲解:孙鑫之掌握C++.md

    **cin与&gt;&gt;一起完成输入操作,cout、cerr与一起完成输出与标准错误输出**。利**用cin和cout比C语言中的scanf和printf要方便得多**,**cin和cout可以自动判别输入输出数据类型而自动调整输入输出格式**,不必像scanf和...

    你必须知道的495个C语言问题

    1.28 文件中的第一个声明就报出奇怪的语法错误,可我看没什么问题。这是为什么? 1.29 为什么我的编译器不允许我定义大数组,如doublearray[256][256]? 命名空间 1.30如何判断哪些标识符可以使用,哪些被保留了...

    《你必须知道的495个C语言问题》

    《你必须知道的495个C语言问题》以问答的形式组织内容,讨论了学习或使用C语言的过程中经常遇到的一些问题。书中列出了C用户经常问的400多个经典问题,涵盖了初始化、数组、指针、字符串、内存分配、库函数、C预...

    C语言精典版本C程序设计语言

    它说明了一个标准库和一个完成输入输出、内存管理和字符串操作等类似任务的函数集扩充。此标准明确地说明了原始定义没有指出的一些特性的行为。同时,此标准还明确地说明了语言中的哪些部分依然依赖于机器。《C程序...

    你必须知道的495个C语言问题(PDF)

    难道在C语言中一个结构不能包含指向自己的指针吗? . . . . 3 1.7 怎样建立和理解非常复杂的声明?例如定义一个包含N 个指向返 回指向字符的指针的函数的指针的数组? . . . . . . . . . . . . . . 3 1.8 函数只定义...

    C语言FAQ 常见问题列表

    难道在C语言中一个结构不能包含指向自己的指针吗? o 2.7 怎样建立和理解非常复杂的声明?例如定义一个包含 N 个指向返回指向字符的指针的函数的指针的数组? o 2.8 函数只定义了一次, 调用了一次, 但编译器提示...

    c语言编写单片机技巧

    此外,C语言程序具有完善的模块程序结构,从而为软件开发中采用模块化程序设计方法提供了有力的保障。因此,使用C语言进行程序设计已成为软件开发的一个主流。用C语言来编写目标系统软件,会大大缩短开发周期,且...

    C程序设计语言 第01章

    而对于编写较大程序所涉及到的一些重要特性,比如指针、机构、C语言中十分丰富的运算符集合、部分控制流语句以及标准库等,本章将暂不做讨论。 这种讲解方式也有缺点。应当提请注意的是,本章的内容中无法找到任何...

    c语言设计Brian W. Kernighan Dennis M. Ritchie

    7.1 标准输入输出 7.2 格式输出—printf函数 7.3 变长变元表 7.4 格式输入—scanf函数 7.5 文件访问 7.6 错误处理—stderr和exit函数 7.7 行输入输出 7.8 其他函数 7.8.1 字符串处理函数 7.8.2 字符类测试...

    C++编程思想习题

    6.5在输入输出流中查找 6.6strstreams 6.6.1为用户分配的存储 6.6.2自动存储分配 6.7输出流格式化 6.7.1内部格式化数据 6.7.2例子 6.8格式化操纵算子 6.9建立操纵算子 6.10输入输出流实例 6.10.1代码生成 6.10.2一个...

    shell 编程(中文)[pdf]

    005_echo_read_cat_管道_tee_标准输入输出和错误_重定向标准输入输出和错误_exec.pdf 006_命令执行顺序.pdf 007_文本过滤_正则表达式.pdf 008_grep家族.pdf 009_awk介绍.pdf 010_sed用法介绍.pdf 011_合并与分割_...

    C 语言编程常见问题解答.chm

    4.4 怎样恢复一个重定向了的标准流? 4.5 stdout能被强制打印到非屏幕设备上吗? 4.6 文本模式(text mode)和二进制模式(binary mode)有什么区别? 4.7 怎样判断是使用流函数还是使用低级函数? 4.8 怎样列出...

    13.第十三章 文件.txt

    C语言中对文件的存取是以字符(字节)为单位的,输入输出数据流的开始和结束仅受程序控制不受物理符号控制(如回车换行符)。输出时不会自动增加回车换行符作为记录结束的标志,输入时不以回车换行符作为记录的间隔...

    C和C++头文件对比一览

    #include &lt;istream&gt; //基本输入流 #include &lt;ostream&gt; //基本输出流 #include &lt;queue&gt; //STL 队列容器 #include &lt;set&gt; //STL 集合容器 #include &lt;sstream&gt; //基于字符串的流 #include &lt;stack&gt; //STL 堆栈容器...

    c程序设计语言入门基础 @精品@ 花我一年才从众书中筛选出的

    花了我将近一年 才从众多 c语言教程书 中筛选出来的精品 目 录 第1章 基本概念 1.1 入门 1.2 变量与算术表达式 1.3 for语句 1.4 符号常量 1.5 字符输入输出 1.5.1 文件复制 1.5.2 字符计数 1.5.3 行...

Global site tag (gtag.js) - Google Analytics