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

如果有用的话,给个免费的赞吧。

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值