资源描述:
《输入输出格式.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、关于输入输出的格式由于课程作业的需要,我们对程序作业的输入输出的格式有一定的要求,每个输入的数据都放在input.txt文件中,输出的数据都存在output.txt文件中。求两个数的和:输入示例输出示例input.txtoutput.txt235下面是一个在标准C++中求和的输入输出的例子:实现的算法#includeusingnamespacestd;intmain(){inta,b;ifstreamfin("input.txt");ofstreamfout("output.txt");fin>
2、>a>>b;fout<intmain(){inta,b;freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);scanf("%d%d",&a,&b);printf("%d",a+b);return0;}(关于freopen函数及stdin、stdout的说明见附录。)然而在线评测系统出于安全因素的考虑,禁止了程序对
3、文件IO方面的权限,在线测试系统中,我们只需将freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);1这两句注释掉即可。说明附录:FILE*freopen(constchar*path,constchar*mode,FILE*stream);Reassignafilepointer.参数说明:pathPathofnewfilemodeTypeofaccesspermittedstreamPointertoFILEstructurefreop
4、enistypicallyusedtoredirectthepre-openedfilesstdin,stdout,andstderrtofilesspecifiedbytheuser.Thenewfileassociatedwithstreamisopenedwithmode,whichisacharacterstringspecifyingthetypeofaccessrequestedforthefile,asfollows:"r"Opensforreading.Ifthefiledoesnotexisto
5、rcannotbefound,thefreopencallfails."w"Opensanemptyfileforwriting.Ifthegivenfileexists,itscontentsaredestroyed."a"Opensforwritingattheendofthefile(appending)withoutremovingtheEOFmarkerbeforewritingnewdatatothefile;createsthefilefirstifitdoesnotexist."r+"Opensf
6、orbothreadingandwriting.(Thefilemustexist.)"w+"Opensanemptyfileforbothreadingandwriting.Ifthegivenfileexists,itscontentsaredestroyed.stdin,stdout,stderr2FILE*stdin;FILE*stdout;FILE*stderr;#includeRemarksThesearestandardstreamsforinput,output,anderror
7、output.Bydefault,standardinputisreadfromthekeyboard,whilestandardoutputandstandarderrorareprintedtothescreen.Thefollowingstreampointersareavailabletoaccessthestandardstreams:PointerStreamstdinStandardinputstdoutStandardoutputstderrStandarderrorThesepointersca
8、nbeusedasargumentstofunctions.Somefunctions,suchasgetcharandputchar,usestdinandstdoutautomatically.Thesepointersareconstants,andcannotbeassignednewvalues.Thefreopenfunctioncanbeusedtoredi