利用freopen()函数和fc命令简化程序调试
大家在参加ACM比赛或者参加c/c++实验技能竞赛的时候,如果遇到大量的输入和大量的输出时,调试起来很不方便。一来如果结果不正确的话,需要重复输入大量数据;二来如果大量输出的话,得仔细检查输出结果与正确答案是否一样。这两项任务有时让人很不舒服。
我们可以利用freopen()函数来重定向流,可以使调试起来更加简单方便。
一个简单的例子:
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int a;
int b;
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout); while(cin>>a>>b)
{
cout<<a+b<<endl;
} return ;
}
在进行输入与输出前,将标准输入流stdin与“in.txt"绑定,标准输出流与"out.txt"绑定。这样,只需在工程文件夹下建立in.txt文件,并将输入写入其中,就可避免每次都要在控制台手动输入了。同理,输出也将输出到out.txt里。
在提交代码时,只需将freopen这两行代码注释掉即可,很方便的。
下面是关于freopen的简介:
FILE * freopen ( const char * filename, const char * mode, FILE * stream );
Reopen stream with different file or mode
Reuses stream to either open the file specified by filename or to change its access mode. If a new filename is specified, the function first attempts to close any file already associated with stream (third parameter) and disassociates it. Then, independently of whether that stream was successfuly closed or not, freopen opens the file specified by filename and associates it with the stream just as fopen would do using the specified mode. If filename is a null pointer, the function attempts to change the mode of the stream. Although a particular library implementation is allowed to restrict the changes permitted, and under which circumstances. The error indicator and eof indicator are automatically cleared (as if clearerr was called). This function is especially useful for redirecting predefined streams like stdin, stdout and stderr to specific files (see the example below).
同时,输出也会输出到out.txt中,如果输出量很大的话,比较起来也是很麻烦的。这时,可以将正确结果也存在另一个记事本中(举例:out1.txt),这样就可以利用windows下的fc命令将程序的输出与正确输出的结果进行比较,也省去一番力气。
将程序的输出与正确结果的比较也有更好的方法,比如我使用的windows下的gvim编辑器,就有这个功能,而且比fc命令好用很多。
利用freopen()函数和fc命令简化程序调试的更多相关文章
- Linux Bash命令关于程序调试详解
转载:http://os.51cto.com/art/201006/207230.htm 参考:<Linux shell 脚本攻略>Page22-23 Linux bash程序在程序员的使 ...
- 使用getopt函数对windows命令行程序进行参数解析
getopt()是libc的标准函数,很多语言中都能找到它的移植版本. // -b -p "c:\input" -o "e:\test\output" bool ...
- 【Linux】GDB程序调试
一.GDB简介 GDB是GNU发布的一款功能强大的程序调试工具.GDB主要完成下面三个方面的功能: 启动被调试程序. 让被调试的程序在指定的位置停住. 当程序被停住时,可以检查程序状态(如变量值) 二 ...
- (转)Java程序利用main函数中args参数实现参数的传递
Java程序利用main函数中args参数实现参数的传递 1.运行Java程序的同时,可以通过输入参数给main函数中的接收参数数组args[],供程序内部使用!即当你在Java命令行后面带上参数,J ...
- php利用wsh突破函数禁用执行命令(安全模式同理)
php利用wsh突破函数禁用执行命令(安全模式同理) 前提.需要服务器支持wsh.并知道php安装目录 但是php利用wsh执行命令是没有asp的权限高的. 突破代码 <?php $cmd= ...
- 利用函数计算构建微信小程序的Server端
10分钟上线 - 利用函数计算构建微信小程序的Server端-博客-云栖社区-阿里云 https://yq.aliyun.com/articles/435430 函数计算 读写 oss import ...
- 黑马程序员——利用swap函数研究C的指针
------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 设计3个函数,分别实现已下功能: 交换两个整数 交换两个整形指针 交换任意两个同类型的变量 #i ...
- posix 线程(一):线程模型、pthread 系列函数 和 简单多线程服务器端程序
posix 线程(一):线程模型.pthread 系列函数 和 简单多线程服务器端程序 一.线程有3种模型,分别是N:1用户线程模型,1:1核心线程模型和N:M混合线程模型,posix thread属 ...
- Linux stat函数和stat命令
stat函数和stat命令 linux文件里的[inode = index node]解释:要理解inode必须了解磁盘和[目录项],inode实际是连接[目录项]和磁盘的中间物质. 图里的大圈代表硬 ...
随机推荐
- sed简单实例练习
sedfile内容如下: Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300 Betty Boop: ...
- sriov查看pf-vf对应关系
自己写的, 方便调试. $ cat pf-vf echo "physfn is $1"echo "pf info:"ls /sys/class/net/$1 - ...
- MVC折线图应用
后台 获取值并转换成json数据存到实体里面,然后前台输出 HighchartsModels model = new HighchartsModels(); model.DataDicJson = J ...
- Dima and Salad(完全背包)
Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- UML视图(四)状态图
以下是一个图书馆管理系统的状态图,非常典型,涵盖状态图的全部元素的使用,由于状态图相对照较简单,直接从看图就能非常好地掌握.假设想对状态图的元素严谨的概念进行了解,在图下方,有仔细的叙述. 看了上面的 ...
- Cooley-Tukey算法 (蝶形算法)
Cooley-Tukey算法差别于其它FFT算法的一个重要事实就是N的因子能够随意选取.这样也就能够使用N=rS的Radix-r算法了.最流行的算法都是以r=2或r=4为基的,最简单的DFT不须要不论 ...
- 如何使用Storyboard创建UIPageViewController
之前我们已经讲过UIPageViewController,那篇文章演示了如何使用Interface Builder创建UIPageViewController.为了适配iOS7和Xcode5,我们重新 ...
- Linux 安装Nginx详细图解教程
进入:/usr/java/nginx位置 下载nginx: wget http://nginx.org/download/nginx-1.8.0.tar.gz 下载openssl : wget htt ...
- HDU 1021 - Fibonacci Again
找规律,分析让 F[N] 每一项对 3 取余的余数: 1,2,0, 2,2,1,0, 1,1,2,0, 2,2,1,0, 1,1,2,0, 2,2,1,0 ......... 显然循环了 #inclu ...
- tcp窗口滑动以及拥塞控制
转自:http://blog.chinaunix.net/uid-26275986-id-4109679.html TCP协议作为一个可靠的面向流的传输协议,其可靠性和流量控制由滑动窗口协议保证,而拥 ...