- 浏览: 1998947 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (651)
- ACE (35)
- BAT (9)
- C/C++ (116)
- fast-cgi (14)
- COM (27)
- python (59)
- CGI (4)
- C# (2)
- VC (84)
- DataBase (29)
- Linux (96)
- P2P (6)
- PHP (15)
- Web (6)
- Memcached (7)
- IME输入法 (11)
- 设计模式 (2)
- 搜索引擎 (1)
- 个人情感 (4)
- 笔试/面试 (3)
- 一亩三分地 (33)
- 历史 (2)
- 地理 (1)
- 人物 (3)
- 经济 (0)
- 不仅仅是笑哦 (43)
- 小故事大道理 (2)
- http://www.bjdsmyysjk120.com/ (0)
- http://www.bjdsmyy120.com/ (0)
- 它山之石可以攻玉 (15)
- 大学生你关注些什么 (28)
- 数据恢复 (1)
最新评论
-
luokaichuang:
这个规范里还是没有让我明白当浏览器上传文件时,STDIN的消息 ...
FastCGI规范 -
effort_fan:
好文章!学习了,谢谢分享!
com技术简介 -
vcell:
有错误os.walk(strPath)返回的已经是全部的文件和 ...
通过python获取目录的大小 -
feifeigd:
feifeigd 写道注意:文章中的CPP示例第二行 #inc ...
ATL入门:利用ATL编写简单的COM组件 -
feifeigd:
注意:文章中的CPP示例第二行 #include " ...
ATL入门:利用ATL编写简单的COM组件
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.
发表评论
-
Berkeley DB 使用经验总结
2012-08-27 14:41 3022作者:陈磊 NoSQL是现在互联网Web2.0时代备受 ... -
嵌入式数据库系统Berkeley DB
2012-08-27 14:37 1471前言 UNIX/LINUX平台下的数据库种类非常多 ... -
Rsync 实现原理
2011-05-12 20:06 8250Rsync 实现原理 前言 关于rsync的原始文档 ... -
c++简单的虚函数测试
2011-04-27 14:25 967#include <iostream> u ... -
C++文件行查找
2011-04-26 14:10 1349#include <iostream> # ... -
c++偏特化简单示例
2011-04-13 11:17 2106c++偏特化 // temp1.c ... -
GDB调试精粹及使用实例
2011-03-16 14:06 1078GDB调试精粹及使用实例 一:列文件清单 1. ... -
简单的ini文件解析
2011-02-12 16:36 1570int GetKeyVal(const string s ... -
scanf族函数高级用法
2011-01-25 16:00 2480如何解释 fscanf(fd,&quo ... -
使用scons替代makefile(1)
2011-01-25 11:58 3637早在多年前我刚开始接触linux下的C程序时,经常被makef ... -
使用scons替代makefile(2)
2011-01-25 11:57 3525本篇文章接着上一篇进一步介绍scons的使用方法,主要介绍静态 ... -
使用scons替代makefile(3)
2011-01-25 11:55 4773在上两篇文章中已经简单介绍了用scons编译库文件,可执行程序 ... -
C 支持动态添加测试数据的测试代码
2011-01-13 17:22 1076/下面的定义为了支持可扩增。 //当需要增加一个新的测试用列 ... -
Linux下Makefile的automake生成
2010-12-28 16:55 1044******************helloworld.c* ... -
SCons笔记(详细版)
2010-12-23 16:11 103711. 基本使用 SConstruct文件就功能而言相当于Ma ... -
scons 学习
2010-12-23 11:14 2106scons 学习 作者:Sam(甄峰) sam_code@h ... -
scons随笔
2010-12-22 20:20 4635scons随笔 Scons是新一代的软件构件工具,或者说ma ... -
Scons在linux下的安装和使用
2010-12-21 11:59 3195因为正在用的一个开源软件需要的Developm ... -
排列组合的实现
2010-12-20 12:41 1009简单算法: 从前往后(或者从后往前)每次交换一个位置。当存在 ... -
UDP编程的服务器 Linux
2010-10-22 18:44 1281UDP编程的服务器端一般步骤是: ...
相关推荐
[例10.3]把命令行参数中的前一个文件名标识的文件, 复制到后一个文件名标识的文件中, 如命令行中只有一个文件名则把该文件写到标准输出文件(显示器)中。 #include main(int argc,char *argv[]) { FILE *fp1,*fp2; ...
但是,在《K&R》中并没有定义一个完整的标准C语言,后来由美国国家标准学会在此基础上制定了一个C 语言标准,于一九八三年发表。通常称之为ANSI C。 当代最优秀的程序设计语言 早期的C语言主要是用于UNIX系统。...
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 ...
**cin与>>一起完成输入操作,cout、cerr与一起完成输出与标准错误输出**。利**用cin和cout比C语言中的scanf和printf要方便得多**,**cin和cout可以自动判别输入输出数据类型而自动调整输入输出格式**,不必像scanf和...
1.28 文件中的第一个声明就报出奇怪的语法错误,可我看没什么问题。这是为什么? 1.29 为什么我的编译器不允许我定义大数组,如doublearray[256][256]? 命名空间 1.30如何判断哪些标识符可以使用,哪些被保留了...
《你必须知道的495个C语言问题》以问答的形式组织内容,讨论了学习或使用C语言的过程中经常遇到的一些问题。书中列出了C用户经常问的400多个经典问题,涵盖了初始化、数组、指针、字符串、内存分配、库函数、C预...
它说明了一个标准库和一个完成输入输出、内存管理和字符串操作等类似任务的函数集扩充。此标准明确地说明了原始定义没有指出的一些特性的行为。同时,此标准还明确地说明了语言中的哪些部分依然依赖于机器。《C程序...
难道在C语言中一个结构不能包含指向自己的指针吗? . . . . 3 1.7 怎样建立和理解非常复杂的声明?例如定义一个包含N 个指向返 回指向字符的指针的函数的指针的数组? . . . . . . . . . . . . . . 3 1.8 函数只定义...
难道在C语言中一个结构不能包含指向自己的指针吗? o 2.7 怎样建立和理解非常复杂的声明?例如定义一个包含 N 个指向返回指向字符的指针的函数的指针的数组? o 2.8 函数只定义了一次, 调用了一次, 但编译器提示...
此外,C语言程序具有完善的模块程序结构,从而为软件开发中采用模块化程序设计方法提供了有力的保障。因此,使用C语言进行程序设计已成为软件开发的一个主流。用C语言来编写目标系统软件,会大大缩短开发周期,且...
而对于编写较大程序所涉及到的一些重要特性,比如指针、机构、C语言中十分丰富的运算符集合、部分控制流语句以及标准库等,本章将暂不做讨论。 这种讲解方式也有缺点。应当提请注意的是,本章的内容中无法找到任何...
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 字符类测试...
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一个...
005_echo_read_cat_管道_tee_标准输入输出和错误_重定向标准输入输出和错误_exec.pdf 006_命令执行顺序.pdf 007_文本过滤_正则表达式.pdf 008_grep家族.pdf 009_awk介绍.pdf 010_sed用法介绍.pdf 011_合并与分割_...
4.4 怎样恢复一个重定向了的标准流? 4.5 stdout能被强制打印到非屏幕设备上吗? 4.6 文本模式(text mode)和二进制模式(binary mode)有什么区别? 4.7 怎样判断是使用流函数还是使用低级函数? 4.8 怎样列出...
C语言中对文件的存取是以字符(字节)为单位的,输入输出数据流的开始和结束仅受程序控制不受物理符号控制(如回车换行符)。输出时不会自动增加回车换行符作为记录结束的标志,输入时不以回车换行符作为记录的间隔...
#include <istream> //基本输入流 #include <ostream> //基本输出流 #include <queue> //STL 队列容器 #include <set> //STL 集合容器 #include <sstream> //基于字符串的流 #include <stack> //STL 堆栈容器...
花了我将近一年 才从众多 c语言教程书 中筛选出来的精品 目 录 第1章 基本概念 1.1 入门 1.2 变量与算术表达式 1.3 for语句 1.4 符号常量 1.5 字符输入输出 1.5.1 文件复制 1.5.2 字符计数 1.5.3 行...