C++读文件技巧

想把多个不同名称的文件读入并输出,可用String s代替freopen的第一个参,还需要s.c_str()才不报语法错。

#include	<bits/stdc++.h>
//#include<cstring>
using namespace std;
string s;
char o;
int a,b,c;
int main(){
	freopen("data.in","w",stdout);
	for (int i=2;i<=9;i++){
		s="game";
		o=i+48;
		s=s+o;
		s=s+".in";
			freopen(s.c_str(),"r",stdin);

		scanf("%d%d%d",&a,&b,&c);
		printf("%d %d %d\n",a,b,c);	
	
	}
	for (int i=1;i<=9;i++){
		s="game1";
		o=i+48;
		s=s+o;
		s=s+".in";
			freopen(s.c_str(),"r",stdin);

		scanf("%d%d%d",&a,&b,&c);
		printf("%d %d %d\n",a,b,c);	
	
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/u014713777/article/details/89504252