当前位置:网站首页>C language: 10. Input stream, output stream, error stream
C language: 10. Input stream, output stream, error stream
2022-07-27 19:33:00 【Brother rabbit head】
c Language :10、 Input stream , Output stream , Error flow
1、 I / O stream · brief introduction
linux Put all the programs , All devices are treated as files .
linux Start a c Language application , It will open by default 3 File stdin、stdout、stderr
Standard input stream stdio
Any application may interact with a file or device , If the program needs to read device information , So it's through stdin Standard input stream to read .
The standard input stream defaults to keyboard .
Standard output stream stdout
stdout It's actually a document , This file can output the output stream to the specified device , For example, network card 、 The printer .
When the standard output stream does nothing , The default is the terminal of the display .( Print data to the display by default )
Standard error flow stderr
2、 Input stream 、 Output stream 、 Error flow demonstration
#include <stdio.h>
int main()
{
// Output stream
//printf("input the value");// The bottom implementation of this line of code is the following code
fprintf(stdout, "input the value\n");
// Input stream
int a;
fscanf(stdin, "%d", &a);//scanf("%d", &a); //fscanf In fact, that is scanf The prototype of the
// Error flow
if (a < 0){
fprintf(stderr, "the value must > 0\n");
return 1;
}
return 0;
}
3、 Redirection mechanism
linux Output stream redirection
Additional :
Use command >> file name The output stream content is appended to the file . Such as ls >> a.txt It means that you will ls The execution result of the command is output to a.txt in , Every execution will go a.txt Add content to .
ls >> a.txt And ls 1>> a.txt Effect of equivalent
Cover :
Use command > file name Overwrite the contents of the output stream to the file . Such as ls > a.txt It means that you will ls The execution result of the command is overwritten a.txt in , Every time you execute a.txt Only the results of the last command execution will be preserved in .
linux Input stream redirection
Suppose there is main.c and input.txt Two documents , The contents are as follows :/main.c
#include <stdio.h>
int main()
{
printf("please input num 1\n");
int num1;
scanf("%d", &num1);
printf("please input num 2\n");
int num2;
scanf("%d", &num2);
printf("num1 + num2 = %d\n", num1 + num2);
return 0;
}
/input.txt
2
2
Execute the command below gcc main.c -o main.out./main.out < input.txt
The results are as follows :
linux Error stream redirection
./a.out 1>t.txt 2>f.txt <input.txt
1>t.txt: Redirect the standard output stream to t.txt file
2>f.txt: Redirect the standard error stream to f.txt file
<input.txt: Redirect the standard input stream to input.txt file
end
Whole linux The operating system is composed of several gadgets ;
In use c Language production system tools , Applet time , Cooperation between multiple tools , If they are connected and run, they must be able to judge the right or wrong execution of the program ;
At this point, the standard input stream 、 Output stream 、 The error flow comes in handy ;
边栏推荐
- HDU1573 X问题【一元线性同余方程组】
- New system installation mysql+sqlyog
- Webmagic+selenium+chromedriver+jdbc垂直抓取数据。
- 换行问题双保险
- 4 轮拿下字节 Offer,面试题复盘
- C语言案例:密码设置及登录> 明解getchar与scanf
- kettle8.2 安装及常见问题
- kettle 合并记录 数据减少
- Mongodb learning notes (1) - install mongodb and its related configurations
- Daily question (02): inverted string
猜你喜欢

2022 preparation for autumn recruitment 10W word interview sketch PDF version, with operating system and computer network interview questions

5W奖金池/面向高校,2022法律科技创新大赛报名火热进行中

编程式跳转

Kettle separate and merge records

原厂PW4203降压型1-3节锂电池充电芯片

c语言:11、管道

Summary of APP launch in vivo application market

正十七边形尺规作图可解性复数证明

c语言:15、结构体

Memory management A4
随机推荐
kettle8.2 安装及常见问题
Anaconda下安装Talib库
Webmagic+selenium+chromedriver+jdbc grabs data vertically.
正十七边形尺规作图可解性复数证明
Can set be used to define local variables in OPDS SQL
Subscription and use of Alibaba cloud video on demand service
Sword finger offer17- print from 1 to the maximum n digits - Analog
c语言:10、输入流,输出流,错误流
Basic network faults and troubleshooting
opds sql 里面可以用set 定义局部变量吗
sql 字段类型转换
MySQL学习笔记(2)——存储过程与存储函数
Automatic testing of Web UI: Selenium syntax explanation is the most complete in the history
SSM project uses filter to realize login monitoring
来一遍《剑指Offer》03. 数组中重复的数字
2022备战秋招10W字面试小抄pdf版,附操作系统、计算机网络面试题
Kettle JVM memory setting - the effect is not obvious
Analysis of Eureka server
SQL field type conversion
How can I get started quickly when I change my career to soft testing and job hopping to a new company?