当前位置:网站首页>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
cccccccc
C 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.
边栏推荐
- C语言文件操作
- 365天挑战LeetCode1000题——Day 039 完全二叉树插入器 + 寻找峰值 II + 快照数组
- 2022数学建模竞赛暑期培训讲座——最优化方法:目标规划
- 冒泡排序 C语言
- 容器安全开源检测工具--问脉 VeinMind(镜像后门、恶意样本、敏感信息、弱口令等)
- QML custom tabbar
- Come on! See how Clickhouse, which has risen 16 places a year, can be implemented in jd.com
- 省市区三级联动(简单又完美)
- NVIDIA Zhou Xijian: the last mile from design to digital marketing
- During the appointment, the 2022 JD cloud industrial integration new product launch was launched online
猜你喜欢
QML type: mousearea
Alibaba cloud architect details nine trends in the game industry
Live broadcast Preview: integration of JD cloud Devops and jfrog product library
Day 5
C语言 一级指针
【赛事预告】云上开发,高效智能——第二届阿里云ECS CloudBuild开发者大赛即将启动
Vs code的安装步骤及环境配置
365天挑战LeetCode1000题——Day 036 二叉树剪枝 + 子数组和排序后的区间和 + 删除最短的子数组使剩余数组有序
More than 200 ISVs have settled in! The first anniversary of Alibaba cloud computing nest
容器安全开源检测工具--问脉 VeinMind(镜像后门、恶意样本、敏感信息、弱口令等)
随机推荐
365天挑战LeetCode1000题——Day 039 完全二叉树插入器 + 寻找峰值 II + 快照数组
英伟达周锡健:设计到数字营销的最后一公里
With frequent data leakage and deletion events, how should enterprises build a security defense line?
510000 prize pool invites you to fight! The second Alibaba cloud ECS cloudbuild developer competition is coming
数据库操作 Day 6
为啥谷歌的内部工具不适合你?
One dimensional array exercise
直播预告|如何通过“智能边缘安全”提升企业免疫力?
【C语言系列】— 不创造第三个变量,实现两个数的交换
Occt learning 002 - environment construction
三次握手四次挥手针对面试总结
分配内存:malloc()和free()
365 day challenge leetcode1000 question - distance between bus stops on day 038 + time-based key value storage + array closest to the target value after transforming the array and + maximum value at t
365天挑战LeetCode1000题——Day 041 二分查找完结纪念 + 第 N 个神奇数字 + 在线选举
Day 1
NVIDIA Zhou Xijian: the last mile from design to digital marketing
直播预告|如何节省30%人工成本,缩短80%商标办理周期?
2022数学建模竞赛暑期培训讲座——最优化方法:目标规划
365 day challenge leetcode 1000 questions - day 037 elements and the maximum side length of squares less than or equal to the threshold + the number of subsequences that meet the conditions
OCCT学习002-----环境搭建