鉴于蒟蒻造数据时的痛苦经历,在这里整理了用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; }
如果有用的话,给个免费的赞吧。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/119653.html