当前位置:网站首页>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 ;
边栏推荐
- An article allows you to master threads and thread pools, and also solves thread safety problems. Are you sure you want to take a look?
- 5W奖金池/面向高校,2022法律科技创新大赛报名火热进行中
- Take byte offer in four rounds and answer the interview questions
- 利用 Fastjson json (简单粗暴版)
- SQL Server top keyword usage
- MySQL learning notes (1) -- variables
- Nacos集群部署-高可用保证
- Using functions to extract numbers from text strings in Excel
- 4 轮拿下字节 Offer,面试题复盘
- C # one method returns multiple values. Suggestions collection
猜你喜欢
随机推荐
c语言:5、多维数组
The go zero singleton service uses generics to simplify the registration of handler routes
X-shell remote connection virtual machine
Automatic testing of Web UI: Selenium syntax explanation is the most complete in the history
27. Basics of golang - mutex lock, read / write lock
Kettle learning - the repository configuration in version 8.2 is grayed out, and there is no connect button
我想咨询下,我们的maxcompute spark程序需要访问redis,开发环境和生产环境redi
c语言:8、makeFile编写
C语言案例:密码设置及登录> 明解getchar与scanf
MySQL learning notes (2) -- stored procedures and stored functions
An article allows you to master threads and thread pools, and also solves thread safety problems. Are you sure you want to take a look?
JS common utils encapsulation
HDU1171_Big Event in HDU【01背包】
Code interview of Amazon
Technology Summit 58 Liu Yuan in the same city was invited to attend qecon 2022 global software quality & effectiveness conference
There is another example of repeater
Basic concepts of Nacos and single machine deployment
v-if,v-else,v-for
IPFs obtains the public key and private key through the interface, and encrypts the storage. First bullet
Analysis of Eureka server








