利用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实际是连接[目录项]和磁盘的中间物质. 图里的大圈代表硬 ...
随机推荐
- 解决GitHub未配置SSH key提示错误信息
git push -u origin master Permission denied (publickey). fatal: Could not read from remote repositor ...
- 采用dlopen、dlsym、dlclose加载动态链接库【总结】
摘自http://www.cnblogs.com/Anker/p/3746802.html 采用dlopen.dlsym.dlclose加载动态链接库[总结] 1.前言 为了使程序方便扩展,具备通 ...
- 最少步数(dfs + bfs +bfs优化)
最少步数 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...
- Codeforces 433 C. Ryouko's Memory Note
C. Ryouko's Memory Note time limit per test 1 second memory limit per test 256 megabytes input stand ...
- cocos2d_android 瞬间动作
该文章所写的瞬间动作主要有CCFlipX,CCFlipY,CCHide,CCShow 当中CCFlipX是以Y轴为中心旋转,CCFlipY是以X轴为中心旋转,CCHide将精灵对象隐藏,CCShow将 ...
- android同时使用多个library时的问题
剧情是这样,我的app要使用四个library,如:LibraryA,LibraryB.这两个库又都需要support.v4.jar. 由于加载的时间不同,所以四个support.v4.jar不同,出 ...
- 谈谈 css 的各种居中——读编写高质量代码有感
css 的居中有水平居中和垂直居中,这两种居中又分为行内元素居中和块级元素居中,不同的居中用不同方法. 水平居中 1.行内元素水平居中(文本,图片) 给父层设置 text-align:center; ...
- the first has precedence, perhaps you need a NameVirtualHost directive
报错信息1: Starting httpd: [Fri May 19 11:49:42 2011] [warn] VirtualHost 127.0.0.1:80 overl aps with Vir ...
- HDU 5785 Interesting
题目: 大概说给一个字符串,找到其所有子串[i...k]满足它是由两个回文串拼成的,求Σi*k. 分析: 用val[1][i]表示以i结尾的回文串的起始位置的和val[0][i]表示以i起始的回文串的 ...
- JavaScript经典代码总结
1. oncontextmenu="window.event.returnvalue=false" 将彻底屏蔽鼠标右键<table border oncontextmenu= ...