freopen多文件输入输出
鉴于蒟蒻造数据时的痛苦经历,在这里整理了用freopen实现多文件输入输出的方法
原理
freopen实现文件输入:
freopen("文件名.in","r",stdin);
freopen实现文件输出:
freopen("文件名.out","w",stdout);
其中,文件名为一个*char 类型的变量,没错,文件名是可以改变的。 因此,我们很容易想到,能不能通过改变文件名来对不同的文件进行操作,即实现多组文件的输入输出。
步骤一
首先我们定义两个*char类型的变量,分别用来存储输入文件名和输出文件名,同时用new char对其分配内存。
char *TheInFileName=new char[8];
char *TheOutFileName=new char[8];
步骤二
接下来我们进行n组循环,每组循环对于不同的文件进行操作。需要注意的是,每次操作完成后需要清空缓存,以免造成下一次操作无法输出的情况。这里,我采用cin.clear()清空。每次循环的时候,需要判断一下文件名(我是从1~100)的大小,根据位数的不同,构建不同的输入文件名和输出文件名。
for(int TheNowLoop=StartTheFileName;TheNowLoop<=EndTheFileName;TheNowLoop++){
if(TheNowLoop<=9){
TheInFileName[0]=char(TheNowLoop+'0');
TheInFileName[1]='.';
TheInFileName[2]='i';
TheInFileName[3]='n';
}else if(TheNowLoop>9&&TheNowLoop<100){
TheInFileName[0]=char(TheNowLoop/10+'0');
TheInFileName[1]=char(TheNowLoop%10+'0');
TheInFileName[2]='.';
TheInFileName[3]='i';
TheInFileName[4]='n';
}else if(TheNowLoop>99&&TheNowLoop<1000){
TheInFileName[0]=char(TheNowLoop/100+'0');
TheInFileName[1]=char((TheNowLoop/10)%10+'0');
TheInFileName[2]=char((TheNowLoop%10)+'0');
TheInFileName[3]='.';
TheInFileName[4]='i';
TheInFileName[5]='n';
}else if(TheNowLoop>999&&TheNowLoop<10000){
TheInFileName[0]=char(TheNowLoop/1000+'0');
TheInFileName[1]=char((TheNowLoop/100)%10+'0');
TheInFileName[2]=char((TheNowLoop/10)%10+'0');
TheInFileName[3]=char((TheNowLoop%10)+'0');
TheInFileName[4]='.';
TheInFileName[5]='i';
TheInFileName[6]='n';
}
if(TheNowLoop<=9){
TheOutFileName[0]=char(TheNowLoop+'0');
TheOutFileName[1]='.';
TheOutFileName[2]='o';
TheOutFileName[3]='u';
TheOutFileName[4]='t';
}else if(TheNowLoop>9&&TheNowLoop<100){
TheOutFileName[0]=char(TheNowLoop/10+'0');
TheOutFileName[1]=char(TheNowLoop%10+'0');
TheOutFileName[2]='.';
TheOutFileName[3]='o';
TheOutFileName[4]='u';
TheOutFileName[5]='t';
}else if(TheNowLoop>99&&TheNowLoop<1000){
TheOutFileName[0]=char(TheNowLoop/100+'0');
TheOutFileName[1]=char((TheNowLoop/10)%10+'0');
TheOutFileName[2]=char(TheNowLoop%10+'0');
TheOutFileName[3]='.';
TheOutFileName[4]='o';
TheOutFileName[5]='u';
TheOutFileName[6]='t';
}else if(TheNowLoop>999&&TheNowLoop<10000){
TheOutFileName[0]=char(TheNowLoop/1000+'0');
TheOutFileName[1]=char((TheNowLoop/100)%10+'0');
TheOutFileName[2]=char((TheNowLoop/10)%10+'0');
TheOutFileName[3]=char(TheNowLoop%10+'0');
TheOutFileName[4]='.';
TheOutFileName[5]='o';
TheOutFileName[6]='u';
TheOutFileName[7]='t';
}
freopen(TheInFileName,"r",stdin);
freopen(TheOutFileName,"w",stdout);
//这里运行程序
work();
cin.clear();//清空缓存
cout.clear();
fclose(stdin);
fclose(stdout);
步骤三
完成work()函数。work()函数内部可以填写任意的代码(当成main()用),所有的输入输出可以使用任意的输入输出方式,printf、scanf、cin、cout、快读快读快输都可以。
完整代码
#include<bits/stdc++.h>
using namespace std;
inline void work(){
//可以使用多种输出方式,包括快读
}
const int StartTheFileName=1;
const int EndTheFileName=100;
inline void MakeFile(){//最多生成10000个(包括0)
char *TheInFileName=new char[8];
char *TheOutFileName=new char[8];
TheInFileName[8]=TheOutFileName[8]='\0';
for(int TheNowLoop=StartTheFileName;TheNowLoop<=EndTheFileName;TheNowLoop++){
if(TheNowLoop<=9){
TheInFileName[0]=char(TheNowLoop+'0');
TheInFileName[1]='.';
TheInFileName[2]='i';
TheInFileName[3]='n';
}else if(TheNowLoop>9&&TheNowLoop<100){
TheInFileName[0]=char(TheNowLoop/10+'0');
TheInFileName[1]=char(TheNowLoop%10+'0');
TheInFileName[2]='.';
TheInFileName[3]='i';
TheInFileName[4]='n';
}else if(TheNowLoop>99&&TheNowLoop<1000){
TheInFileName[0]=char(TheNowLoop/100+'0');
TheInFileName[1]=char((TheNowLoop/10)%10+'0');
TheInFileName[2]=char((TheNowLoop%10)+'0');
TheInFileName[3]='.';
TheInFileName[4]='i';
TheInFileName[5]='n';
}else if(TheNowLoop>999&&TheNowLoop<10000){
TheInFileName[0]=char(TheNowLoop/1000+'0');
TheInFileName[1]=char((TheNowLoop/100)%10+'0');
TheInFileName[2]=char((TheNowLoop/10)%10+'0');
TheInFileName[3]=char((TheNowLoop%10)+'0');
TheInFileName[4]='.';
TheInFileName[5]='i';
TheInFileName[6]='n';
}
if(TheNowLoop<=9){
TheOutFileName[0]=char(TheNowLoop+'0');
TheOutFileName[1]='.';
TheOutFileName[2]='o';
TheOutFileName[3]='u';
TheOutFileName[4]='t';
}else if(TheNowLoop>9&&TheNowLoop<100){
TheOutFileName[0]=char(TheNowLoop/10+'0');
TheOutFileName[1]=char(TheNowLoop%10+'0');
TheOutFileName[2]='.';
TheOutFileName[3]='o';
TheOutFileName[4]='u';
TheOutFileName[5]='t';
}else if(TheNowLoop>99&&TheNowLoop<1000){
TheOutFileName[0]=char(TheNowLoop/100+'0');
TheOutFileName[1]=char((TheNowLoop/10)%10+'0');
TheOutFileName[2]=char(TheNowLoop%10+'0');
TheOutFileName[3]='.';
TheOutFileName[4]='o';
TheOutFileName[5]='u';
TheOutFileName[6]='t';
}else if(TheNowLoop>999&&TheNowLoop<10000){
TheOutFileName[0]=char(TheNowLoop/1000+'0');
TheOutFileName[1]=char((TheNowLoop/100)%10+'0');
TheOutFileName[2]=char((TheNowLoop/10)%10+'0');
TheOutFileName[3]=char(TheNowLoop%10+'0');
TheOutFileName[4]='.';
TheOutFileName[5]='o';
TheOutFileName[6]='u';
TheOutFileName[7]='t';
}
freopen(TheInFileName,"r",stdin);
freopen(TheOutFileName,"w",stdout);
//这里运行程序
work();
cin.clear();//清空缓存
cout.clear();
fclose(stdin);
fclose(stdout);
}
}
int main(){
MakeFile();
return 0;
}
如果有用的话,给个免费的赞吧。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示