信息学奥赛中文件流的写法
头文件
#include<cstdio>
也可以用万能头
格式如下:
int main(){
freopen("xxxx.in","r",stdin);
freopen("xxxx.out","w",stdout);
/*代码*/
fclose(stdin);
fclose(stdout);
retrun 0;
}其中, r是read的缩写,w是write的缩写。
xxxx就是你要替换掉的内容,比如下面这道题。
可以看到一共四道题,每道题目都有一个英文名字。而且给出了输入文件名和输出文件名。
对于第一题成绩来说,我们的代码应该如下:
int main(){
freopen("score.in","r",stdin);
freopen("score.out","w",stdout);
/*你的代码*/
fclose(stdin);
fclose(stdout);
retrun 0;
}对于第二题,应该 如下:
int main(){
freopen("librarian.in","r",stdin);
freopen("librarian.out","w",stdout);
/*你的代码*/
fclose(stdin);
fclose(stdout);
retrun 0;
}需要注意的是,如果你写了文件流,去调试代码的时候,你是无法输入的。所以,我们在测试我们代码的时候会把文件流暂时注释掉,比如下面这样:
int main(){
//freopen("score.in","r",stdin);
//freopen("score.out","w",stdout);
/*你的代码*/
//fclose(stdin);
//fclose(stdout);
retrun 0;
}等觉得程序没有问题了,可以提交了,就取消注释即可。
int main(){
freopen("score.in","r",stdin);
freopen("score.out","w",stdout);
/*你的代码*/
fclose(stdin);
fclose(stdout);
retrun 0;
}扫描二维码推送至手机访问。
版权声明:本文由青少年编程知识记录发布,如需转载请注明出处。


