#include"stdafx.h"
#include"iostream"
#include"string"
#include"fstream"
using namespace std;
typedef struct Stu{
string name;
string number;
int score1;
int score2;
int score3;
struct Stu * next;
}Student,*StudentPtr;
class StudentSystem{
private:
StudentPtr L;
public:
StudentSystem(){
StudentPtr p=new Student;
p->next=NULL;
fstream txtfile;
txtfile.open("e:\\Student.txt",ios::in|ios::out);
if(!txtfile){
cerr<<"文件生成失败"<<endl;
exit(1);
}
//文件内容抽取
while(!txtfile.eof()){
StudentPtr a;
a=new Student;
txtfile>>a->name>>a->number>>a->score1>>a->score2>>a->score3;
a->next=p;
p=a;
};
txtfile.close();
//删除含有无效信息的节点
if(!p->next->next){
StudentPtr a;
a=p;
p=p->next;
delete(a);
}
else{
StudentPtr f=p;
StudentPtr a=f->next;
while(!a->next->next){
f=f->next;
a=f->next;
}
f->next=a->next;
delete(a);
}
L=p;
int choose;
do{
cout<<"请选择操作:1-插入学生;2-显示全部学生;3-显示指定学生;4-修改指定学生信息;5-学生排序"<<endl;
cin>>choose;
switch(choose){
case 1:
insert_student();
break;
case 2:
showall_student();
break;
case 3:
showone_student();
break;
case 4:
change_student();
break;
case 5:
order_student();
break;
default:
cout<<"选择失败"<<endl<<endl;
break;
}
cout<<"是否继续操作(1:继续选择其他操作 0:退出)"<<endl;
cin>>choose;
}while(choose==1);
};
~StudentSystem(){
fstream txtfile;
txtfile.open("e:\\Student.txt",ios::out|ios::trunc);
if(!txtfile)
{
cerr<<"文件保存失败"<<endl;
exit(1);
}
StudentPtr p=L;
while(p->next!=NULL){
txtfile<<p->name<<"\t"<<p->number<<"\t"<<p->score1<<"\t"<<p->score2<<"\t"<<p->score3<<"\n";
p=p->next;
}
txtfile.close();
}
StudentPtr search_student();
void insert_student();
void showall_student();
void showone_student();
void change_student();
void order_student();
void order();
void order(string a,string b);
void order(string a,int b);
void order(int a);
void order(int a,int b);
void order(int a,int b,int c);
void order(int a,int b,int c,int d);
};
C++文件打开一直失败
答案:2 悬赏:0
解决时间 2021-04-21 03:33
- 提问者网友:回憶丶初
- 2021-04-20 23:50
最佳答案
- 二级知识专家网友:转身→时光静好
- 2021-04-21 01:03
可以改成 txtfile.open("e:\\Student.txt",ios::in|ios::out|ios::app);
但我不知道这符不符合你的需求
但我不知道这符不符合你的需求
全部回答
- 1楼网友:duile
- 2021-04-21 02:38
可能的几个原因有:
1. 路径下的文件不存在
2. 执行环境没有管理员权限无法操作指定文件
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯