当前位置:网站首页>c语言:10、输入流,输出流,错误流
c语言:10、输入流,输出流,错误流
2022-07-27 16:47:00 【兔头哥哥】
c语言:10、输入流,输出流,错误流
1、输入输出流·简介
linux把所有程序,所有设备都当做文件来处理。
linux启动一个c语言应用程序的时候,会默认打开3个文件stdin、stdout、stderr
标准输入流stdio
任何一个应用程序都有可能要和一个文件或者设备进行交互,如果程序需要读取设备信息,那么就是通过stdin标准输入流来读取。
标准输入流默认是键盘。
标准输出流stdout
stdout其实是一个文件,该文件可以将输出流输出到指定的设备中,比如网卡、打印机。
标准输出流不做任何操作时,默认是显示器的终端。(默认向显示器打印数据)
标准错误流stderr
2、输入流、输出流、错误流演示
#include <stdio.h>
int main()
{
//输出流
//printf("input the value");//这行代码底层实现就是下方代码
fprintf(stdout, "input the value\n");
//输入流
int a;
fscanf(stdin, "%d", &a);//scanf("%d", &a); //fscanf其实就是scanf的原型
//错误流
if (a < 0){
fprintf(stderr, "the value must > 0\n");
return 1;
}
return 0;
}
3、重定向机制
linux输出流重定向
追加:
使用命令 >> 文件名的方式将输出流内容追加到文件中。如ls >> a.txt表示将ls命令的执行结果输出到a.txt中,每次执行都会往a.txt中追加内容。
ls >> a.txt与ls 1>> a.txt效果等价
覆盖:
使用命令 > 文件名的方式将输出流内容覆写到文件中。如ls > a.txt表示将ls命令的执行结果覆写到a.txt中,每次执行a.txt中只会保留上次命令执行的结果。
linux输入流重定向
假设有main.c和input.txt两个文件,内容如下:/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
执行下方命令gcc main.c -o main.out./main.out < input.txt
执行结果如下:
linux错误流重定向
./a.out 1>t.txt 2>f.txt <input.txt
1>t.txt:将标准输出流重定向到t.txt文件
2>f.txt:将标准错误流重定向到f.txt文件
<input.txt:将标准输入流重定向为input.txt文件
终
整个linux操作系统就是由若干个小工具组成的;
在用c语言制作系统工具,小应用程序时,多个工具之间要合作,相互衔接运行的话就必须能够判断程序执行的对错;
此时标准输入流、输出流、错误流就派上用场了;
边栏推荐
- JMeter interface automation - how to solve the content type conflict of request headers
- Some advice for NS2 beginner.
- How to break the team with automated testing
- sql 字段类型转换
- opds sql 里面可以用set 定义局部变量吗
- ipfs通过接口获得公钥、私钥,并加密存储。第一弹
- Kettle JVM memory setting - the effect is not obvious
- X-shell remote connection virtual machine
- Use fastjson JSON (simple and rough version)
- kettle学习——8.2版本的资源库配置变为灰色,且没有了Connect按钮
猜你喜欢

The go zero singleton service uses generics to simplify the registration of handler routes

每日一题(02):倒置字符串

Opening and using Alibaba cloud object storage OSS

成本高、落地难、见效慢,开源安全怎么办?

Kettle references external scripts to complete phone number cleaning, de duplication and indentation

sql 字段类型转换

Definition of graph traversal and depth first search and breadth first search (2)

Rs2022/ cloud detection: semi supervised cloud detection in satellite images by considering the

Unity learning notes (rigid body physics collider trigger)

低代码实现探索(四十五)业务参数
随机推荐
Webmagic+selenium+chromedriver+jdbc grabs data vertically.
Debian夺回“debian.community“ 域名,喷子仍不善罢甘休
C language case: password setting and login > clear solution getchar and scanf
Power control
What if idea successfully connects to the database without displaying the table
kettle引用外部脚本完成电话号码清洗、去重缩进
Using functions to extract numbers from text strings in Excel
Using MATLAB to generate graphics for journals and conferences - plot
Latex use - subfigure vertical graphics
Analysis of Eureka server
Latex use - control the display position of tables or graphics
Definition of graph traversal and depth first search and breadth first search (2)
Nacos cluster deployment - high availability guarantee
Summary of "performance test" of special test
Usage of ref keyword
Memory management A4
MySQL learning notes (2) -- stored procedures and stored functions
Self control principle learning notes - system stability analysis (1) - BIBO stability and Routh criterion
The understanding of string in C.
每日一题(02):倒置字符串