文件类型是.txt的
程序如下:#include "stdafx.h"
#include "iostream"
#include "string"
#include "fstream"
#include "time.h"
#include "stdlib.h"
using namespace std;
class Student{
string Name;//学生姓名
string ID;//学生学号
string Number;//学生序号
public:
string GetName();
string GetNumber();
string GetID();
friend class Class;
};
string Student::GetID()
{
return ID;
}
string Student::GetName()
{
return Name;
}
string Student::GetNumber()
{
return Number;
}
class System
{
public:
void menu();
};
class Class{
Student *student;
int number;
public:
Class(string);
~Class(){delete []student;};
int GetNumber();
friend void System::menu();
};
void System::menu()
{
string temp;
cout<<"请输入学生名单文件(*.txt)的全路径(输入exit为退出本程序):";
cin>>temp;
if(temp=="exit")exit(0);
Class cls(temp);
char tmp,tp;
loop: cout<<"Enter 1 or 0<1抽取,0退出,其它无效>:";
cin>>tmp;
if(tmp=='1')
{
int NUM;//被选中学生的号码
int b;
int number=0;
char *p=new char[100];
fstream File;
File.open(temp.c_str());
while(!File.eof()){File.getline(p,100);number++;}
b=number;
File.close();
again:srand((unsigned)time(NULL));
NUM=(rand()%(b-2))+1;
cout<<"被抽中的是:"<<endl;
cout<<"学生序号:"<<cls.student[temp[NUM]].GetNumber()<<endl;
cout<<"学生学号:"<<cls.student[temp[NUM]].GetID()<<endl;
cout<<"学生姓名:"<<cls.student[temp[NUM]].GetName()<<endl;
againchoose: cout<<"Enter 1 or 0<1为继续抽取,0为退出,其它无效>:";
cin>>tp;
if(tp=='1')goto again;
else if(tp=='0')exit(0);
else goto againchoose;
}
else if(tmp=='0')exit(0);
else goto loop;
}
Class::Class(string temp)
{
begin:string tmp;
number=0;
char *p=new char[100];
while(1)
{
fstream File;
File.open(temp.c_str());
if(File.good())
{
cout<<"文件打开成功......"<<endl;
while(!File.eof()){File.getline(p,100);number++;}
cout<<number<<endl;
student=new Student[number];
File.seekg(0, ios_base::beg);
for(int i=0;i<number;i++)
{
File>>student[i].Number>>student[i].ID>>student[i].Name;
}
File.close();
break;
}
else
{
cout<<"Warnning:Cannot open this file!"<<endl;
File.close();
cout<<"请重新输入路径:"<<endl;
cin>>temp;
goto begin;
}
}
cout<<"已经初始化....."<<endl;
cout<<"该班学生人数为:"<<number<<"人"<<endl;
}
int Class::GetNumber()
{
return number;
}
int main(int argc, char* argv[])
{
System sys;
sys.menu();
return 0;
}
断点调试时Name、ID、Number的Value都显示为{0x00000000 ""}