前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C、C++ 标准输入重定向 & 万能头 - 编程技巧

C、C++ 标准输入重定向 & 万能头 - 编程技巧

作者头像
xuyaowen
发布2020-12-30 14:33:18
6300
发布2020-12-30 14:33:18
举报
文章被收录于专栏:XUYAOWEN的专栏XUYAOWEN的专栏

转眼到了毕业季,大家都在忙着找暑期实习;我也投了一个,是阿里巴巴的暑期实习;实习,少不了机试,又想起了大一时曾经凑过acm的热闹;当时学到一个技巧,是使用重定向向输入输出函数,这样在进行测试的时候就比较方便了;

这样调试的时候,不用从控制台进行手动输入,直接从文件中进行输入就行;这样也方便debug;

引入头文件:

代码语言:javascript
复制
#include <cstdio>

经典使用样例:(我一般只重定向输入,还是在标准输入中输出)

代码语言:javascript
复制
freopen("in.txt","r",stdin);  
freopen("out.txt","w",stdout); 

函数解释:(可以使用 man freopen 进行查询)

代码语言:javascript
复制
#include <stdio.h>
FILE *
freopen(const char *path, const char *mode, FILE *stream);
代码语言:javascript
复制
The freopen() function opens the file whose name is the string pointed to by path and associates the stream pointed to by stream
with it.  The original stream (if it exists) is closed.  The mode argument is used just as in the fopen() function.
If the path argument is NULL, freopen() attempts to re-open the file associated with stream with a new mode.  The new mode must be
compatible with the mode that the stream was originally opened with: Streams open for reading can only be re-opened for reading,
streams open for writing can only be re-opened for writing, and streams open for reading and writing can be re-opened in any mode.
The ``x'' mode option is not meaningful in this context.
The primary use of the freopen() function is to change the file associated with a standard text stream (stderr, stdin, or stdout).

另外一个技巧就是,c++的万能头:

代码语言:javascript
复制
#include<bits/stdc++.h> 

部分平台,不支持此头文件;经过我的测试,macos 的clang++ 11.0.0 好像不支持这个文件;但是g++ 9.3.0通过我的测试,好像支持这个文件;

保持更新,转载请注明出处;更多内容请关注cnblogs.com/xuyaowen; 

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-03-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档