|
10鱼币
#include <iostream>
#include <fstream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct student
{
string name;
string kemu;
char chengji;
};
bool instudent();
bool readstudent();
void recordstudent();
bool writestudent(student *studentdata);
int main()
{
int i;
instudent();//初始化数据。
while(1)
{
cout<<"请选择需要进行的操作\n";
cout<<"1.打印数据到屏幕\n";
cout<<"2.写入数据\n";
cout<<"3.程序结束\n";
cin>>i;
switch(i)
{
case 1:
if(readstudent())
cout<<"成功读取文件\n";
else
cout<<"读取文件失败\n";
break;
case 2:
recordstudent();
break;
case 3:
return 0;
}
}
cout<<"初始化失败!!!\n\n";
return 0;
}
bool instudent()
{
student newstudent = {"洪耀文","C++",'A'};
}
bool readstudent()
{
string tem;
ifstream fileopen("E:\\1.txt");
if(fileopen.is_open())
{
cout<<" 姓名 "<<" 科目 "<<" 成绩 "<<"\n\n";
while(getline(fileopen,tem))
{
cout<<tem<<" ";
cout<<"\n";
}
cout<<"\n\n";
return true;
}
else
return false;
}
void recordstudent()
{
char jixu,save;
student studentdata;
student *pstudentdata;
jixu='y';
while('y'==jixu)
{
cout<<"请输入学生姓名";
cin>>studentdata.name;
cout<<"请输入科目:";
cin>>studentdata.kemu;
cout<<"请输入学生科目成绩:";
cin>>studentdata.chengji;
cout<<"录入成功,需要保存吗?(Y/N)";
cin>>save;
if('Y'==save)
{
pstudentdata = &studentdata;
if(writestudent(pstudentdata))
{
cout<<"成功录入文件\n";
}
else
cout<<"录入文件失败\n";
}
else
return;
}
cout<<"需要再次录入么?(Y/N)";
cin>>jixu;
}
bool writestudent(student *studentdata)
{
ofstream fileout("E:\\1.txt",std::ios::app); //std::ios::app是在老数据后添加新数据
if(fileout.is_open())
{
fileout<<pstudentdata->name<<" ";
fileout<<pstudentdata->kemu<<" ";
fileout<<pstudentdata->chengji<<"\n";
fileout.close();
cout<<"数据保存成功\n\n";
}
else
cout<<"数据保存失败\n\n";
}
|
最佳答案
查看完整内容
其实我好多地方都不懂有什么用。可以说说具体吗?我想帮你改,可是不知道怎么改好。
现在只是调通了,你或者看看,有没有帮助吧,希望对你有帮助。
#include
#include
#include //用到string的话一定要加这个头文件
using namespace std;
/* run this program using the console pause or add your own getch, system("pause") or input loop */
struct student
{
string name;
string k ...
|