当前位置:网站首页>Redirection and files
Redirection and files
2022-07-29 05:28:00 【Ryan fitter】
Redirects and files
Programs can use files in two ways :
The first way is , Show opening a file with a specific function , Close file , Read the file , write file .
The second way is , Design programs that interact with keyboards and items , Redirect input value files and output from files through different channels . In other words , hold stdin Stream reassigned to file .
A major problem with redirection is that it is related to the operating system , And C irrelevant .
1. Redirect input
Redirect the input such as the operation instruction is "<"
Because it uses VSCode When running the program, you can input ./ecoh_eof Instructions
You can execute the file, but you need to compile the link generation before that .exe file .
When reading the book, the redirected input says words Text file for , At first I thought it was .txt The format of , But I'm trying VScode Its own terminal 、git bash And the system comes with Powershell The effect cannot be displayed after .

As shown in the figure above .
Later, it was found that the reason for the problem was .txt The reason for the document . So I deleted .txt After running the program, it will have an effect .
stay words Enter... In the file
aaaaaaaa
bbbbbbbb
ccccccccC The procedure is :
#include <stdio.h>
int main(void)
{
int ch;
while((ch = getchar()) != EOF)
putchar(ch);
return 0;
}But in VSCode The terminal running result on will still report an error, so it is replaced by git bash Up operation .
The operation effect is as follows :

The successful running , This well proves that the above input problems are the cause of the operating system , because git bash Is similar to Linux Of .
2. Redirect output
The instructions used in the redirect output operation are ">"
Also in git bash Input in ./ecoh_eof >savewords
You can save the input characters to savewords On the file . But it's not clear git bash How did you quit editing text in , The book says Ctrl+z(DOS) or Ctrl+D(UNIX), I tried two methods, but I couldn't return the train , Then I switched back VSCode Interestingly VSCode Your terminal can use ">"
Then the display results are as follows :

3. Combined redirection
One thing to note about combined redirection is that the input file name and output file name cannot be the same in a command . The reason is that the length of the original file has been truncated before the output file is input. Similarly, the following commands :
./echo_eof < mywords > mywords
The redirected command is independent of the order of the redirected operators. For example, the execution effect of the following two commands is the same .
./echo_eof < mywords > savewords
./echo_eof > savewords < mywords

The effect of the two implementations is the same .
Points for attention when using redirection
1. Redirection operator connects an executable ( Including standard operating system commands ) And a data file , Cannot be used to connect one data file to another , Nor can it be used to connect one program to another .
2. You cannot read the input of multiple files using the redirection operator , You can't direct output to multiple files .
3. Usually , Spaces between file names and operators are not required , Except occasionally in UNIX shell、Linux shell or Windows.
边栏推荐
- 携手数字人、数字空间、XR平台,阿里云与伙伴共同建设“新视界”
- 365 day challenge leetcode 1000 questions - day 039 full binary tree inserter + find peak II + snapshot array
- Complete ecological map of R & D Efficiency & selection of Devops tools
- 千人规模互联网公司研发效能成功之路
- 365天挑战LeetCode1000题——Day 035 每日一题 + 二分查找 13
- MySQL的基础概念+数据库系统结构+拓展延申+基础命令学习
- With cloud simulation platform, Shichuang technology supports the upgrading of "China smart manufacturing"
- 365天挑战LeetCode1000题——Day 040 设计跳表 + 避免洪水泛滥 + 查找大小为 M 的最新分组 + 销售价值减少的颜色球
- 2022数学建模竞赛暑期培训讲座——最优化方法:目标规划
- 容器安全开源检测工具--问脉 VeinMind(镜像后门、恶意样本、敏感信息、弱口令等)
猜你喜欢

365 day challenge leetcode 1000 questions - day 041 two point search completion anniversary + nth magic number + online election

365天挑战LeetCode1000题——Day 040 设计跳表 + 避免洪水泛滥 + 查找大小为 M 的最新分组 + 销售价值减少的颜色球

CMU15-213 Shell Lab实验记录

Alibaba cloud architect Liang Xu: MES on cloud box helps customers quickly build digital factories

研发效能|Kubernetes核心技术剖析和DevOps落地经验

千人规模互联网公司研发效能成功之路

【C语言系列】— 不创造第三个变量,实现两个数的交换

预约中,2022京东云产业融合新品发布会线上开启

来!看排名一年上升16位的ClickHouse,如何在京东落地实践

省市区三级联动(简单又完美)
随机推荐
365 day challenge leetcode 1000 questions - day 040 design jump table + avoid flooding + find the latest grouping with size M + color ball with reduced sales value
Yangyonglin, vice president of Rushi Technology: when traditional industries encounter "digital space"
With frequent data leakage and deletion events, how should enterprises build a security defense line?
京东云联合Forrester咨询发布混合云报告 云原生成为驱动产业发展新引擎
冒泡排序 C语言
QML control: combobox
【C语言系列】—深度解剖数据在内存中的存储(一) 暑假开篇
时间复杂度和空间复杂度
牛客网编程题—【WY22 Fibonacci数列】和【替换空格】详解
浅谈范式
C language handwritten qq-ai version
省市区三级联动(简单又完美)
QML type: state state
第一周总结
三次握手四次挥手针对面试总结
来!看排名一年上升16位的ClickHouse,如何在京东落地实践
CryEngine5 Shader调试
D3d Shader Instruction
510000 prize pool invites you to fight! The second Alibaba cloud ECS cloudbuild developer competition is coming
【C语言系列】— 把同学弄糊涂的 “常量” 与 “变量”