利用freopen()函数和fc命令简化程序调试
2014-04-04 17:02
323 查看
大家在参加ACM比赛或者参加c/c++实验技能竞赛的时候,如果遇到大量的输入和大量的输出时,调试起来很不方便。一来如果结果不正确的话,需要重复输入大量数据;二来如果大量输出的话,得仔细检查输出结果与正确答案是否一样。这两项任务有时让人很不舒服。
我们可以利用freopen()函数来重定向流,可以使调试起来更加简单方便。
一个简单的例子:
在进行输入与输出前,将标准输入流stdin与“in.txt"绑定,标准输出流与"out.txt"绑定。这样,只需在工程文件夹下建立in.txt文件,并将输入写入其中,就可避免每次都要在控制台手动输入了。同理,输出也将输出到out.txt里。
在提交代码时,只需将freopen这两行代码注释掉即可,很方便的。
下面是关于freopen的简介:
View Code
同时,输出也会输出到out.txt中,如果输出量很大的话,比较起来也是很麻烦的。这时,可以将正确结果也存在另一个记事本中(举例:out1.txt),这样就可以利用windows下的fc命令将程序的输出与正确输出的结果进行比较,也省去一番力气。
将程序的输出与正确结果的比较也有更好的方法,比如我使用的windows下的gvim编辑器,就有这个功能,而且比fc命令好用很多。
我们可以利用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 0; }
在进行输入与输出前,将标准输入流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).
View Code
同时,输出也会输出到out.txt中,如果输出量很大的话,比较起来也是很麻烦的。这时,可以将正确结果也存在另一个记事本中(举例:out1.txt),这样就可以利用windows下的fc命令将程序的输出与正确输出的结果进行比较,也省去一番力气。
将程序的输出与正确结果的比较也有更好的方法,比如我使用的windows下的gvim编辑器,就有这个功能,而且比fc命令好用很多。
相关文章推荐
- 面试准备(常用函数和数据结构)freopen (在程序调试时从文本输入,程序结果放在文本中)
- 如何利用gdb调试程序之细节(info reg命令以及寄存器地址)
- Python利用hadoop Streaming编写的Map-Reduce程序命令运行和本地调试运行
- 程序调试的时候利用Call Stack窗口查看函数调用信息
- 游戏调试:利用双计算机调试游戏程序(转)
- 使用 Linux 的 strace 命令跟踪/调试程序的常用选项
- 第三周程序阅读(3) 利用函数访问私有数据成员
- Moto Linux应用开发(三):利用samba和telnet简化调试工作
- 利用Console来调试JS程序、Console用法总结
- android利用日志消息调试程序
- 利用__FILE__, __LINE__, __FUNCTION__跟踪调试程序
- 利用ubuntu的alias命令来简化许多复杂难打的命令
- 利用JS函数制作时钟运行程序
- 利用gdb在汇编指令级调试C程序
- 利用ATL创建com组件和如何在程序中使用组件的接口函数和设置接口的属性
- FC命令查看安装程序时都向注册表干了些什么
- 调试Flex 4程序-- 第三部分:变量视图,观察点,和定位到指定行命令
- shell中利用自定义命令简化操作
- c++ 类模板及exe程序怎么利用obj文件去寻找函数地址原理
- usb调试停用系统应用-利用adb命令