#include<iostream>
#include<fstream>
#include<cstring>
#include<vector>
#include<ctime>
#include<cstdlib>
using namespace std;
class stu{
	private:
		int order;
		string id,name,cla;
	public:
		stu(const stu &p);
		stu(int orderx,string idx,string namex,string clax);
		void print();
		int geto(){return order;}
		string geti(){return id;}
		string getn(){return name;}
		string getc(){return cla;}
};
stu::stu(const stu &p){
	order=p.order;
	id=p.id;
	name=p.name;
	cla=p.cla;
}
stu::stu(int orderx,string idx,string namex,string clax):order(orderx),id(idx),name(namex),cla(clax){}
void stu::print(){
	cout<<order<<" "<<id<<" "<<name<<" "<<cla<<endl;
}
int main(){
	vector<stu> stus;
	int order;
	string id,name,cla;
	char ch;//读取上一行的换行符 
	string filename;
	cout<<"输入要打开的班级"<<endl;
	cin>>filename; 
	ifstream fin(filename);
	if(!fin){
		cout<<"fail to open list.txt"<<endl;
		return 1;
	}
	while(fin>>order>>id>>name>>cla){
		fin.get(ch);
        stus.push_back(stu(order,id,name,cla)) ;
}
		fin.close();
    time_t t = time(0); 
    char tmp[64]; 
    strftime( tmp, sizeof(tmp), "%Y%m%d%H%M%S.txt",localtime(&t) );
    string filename2=tmp;
	ofstream fout(filename2);
	if(!fout){
		cout<<"fail to open"<<endl;
		return 1;
	}
	srand(time(NULL));
	int n;
	cout<<"继续点名输入1,否则输入2"<<endl; 
	while(cin>>n){
		if(n==1){
	int j=rand()%stus.size();
    
	stu exm(stus[j]);
	exm.print();
	fout<<exm.geto()<<" "<<exm.geti()<<" "<<exm.getn()<<" "<<exm.getc()<<endl;
}
    else
    
    break;
	}
	fout.close() ;
	return 0;
} 
  

原文:https://www.cnblogs.com/-19990406-whhw/p/9193361.html