日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

利用freopen()函数和fc命令简化程序调试

發(fā)布時間:2025/5/22 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用freopen()函数和fc命令简化程序调试 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

  大家在參加ACM比賽或者參加c/c++實驗技能競賽的時候,如果遇到大量的輸入和大量的輸出時,調(diào)試起來很不方便。一來如果結(jié)果不正確的話,需要重復(fù)輸入大量數(shù)據(jù);二來如果大量輸出的話,得仔細(xì)檢查輸出結(jié)果與正確答案是否一樣。這兩項任務(wù)有時讓人很不舒服。

  我們可以利用freopen()函數(shù)來重定向流,可以使調(diào)試起來更加簡單方便。

  一個簡單的例子:

#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; }

  在進(jìn)行輸入與輸出前,將標(biāo)準(zhǔn)輸入流stdin與“in.txt"綁定,標(biāo)準(zhǔn)輸出流與"out.txt"綁定。這樣,只需在工程文件夾下建立in.txt文件,并將輸入寫入其中,就可避免每次都要在控制臺手動輸入了。同理,輸出也將輸出到out.txt里。

  在提交代碼時,只需將freopen這兩行代碼注釋掉即可,很方便的。

  下面是關(guān)于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中,如果輸出量很大的話,比較起來也是很麻煩的。這時,可以將正確結(jié)果也存在另一個記事本中(舉例:out1.txt),這樣就可以利用windows下的fc命令將程序的輸出與正確輸出的結(jié)果進(jìn)行比較,也省去一番力氣。

  將程序的輸出與正確結(jié)果的比較也有更好的方法,比如我使用的windows下的gvim編輯器,就有這個功能,而且比fc命令好用很多。

轉(zhuǎn)載于:https://www.cnblogs.com/wangaohui/p/3645510.html

總結(jié)

以上是生活随笔為你收集整理的利用freopen()函数和fc命令简化程序调试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。