#include <iostream> #include <cstring> #include <fstream> #include <cstdlib> using namespace std; struct student{ // 创建构造体 int num; char name[20]; ....; }; int main(){ student.stud; char filename[40]; fstream infile; // (infile是名称) fstream类,它是从iostream类派生的。 用来支持对磁盘文件的输入输出。 cout<<"please input a filename:"; cin >>filename; infile.open(filename , ios::in); //打开文件 if(!infile){ //打不开报错退出 cout<<"can not open file: "<<filename<<endl; exit(1); } infile >> stud.num; //输入 infile >> stud.name; // 输入 ...; }