将客 2024-06-15 23:09 采纳率: 96.6%
浏览 1
已结题

关于#c++#的问题:输入为这样temp文件里面的内容为这样如果我只想让temp文件里面的内只有一行,并且里面的出生日期和成绩是修改后的


#include<fstream>
#include<iostream>
#include<string>
#include<cstdlib>
#include<vector>
#include<algorithm>
using namespace std;
//定义时间日期类表示学生的出生年月日
class Date
{
public:
void SetDate () //设置年月
{
cout<<"请输入出生年份:";
cin>>year;
cout<<"请输入出生月份:";
cin>>month;
cout<<"请输入出生日:";
cin>>day;
}
int GetYear()
 {return year;}//获取年份
int GetMonth() 
{return month;}//获取月
int GetDay()
 {return day;} //获取日
void SetBirthDate()
{SetDate();}
private:
int year,month,day;
};
//定义学生类,包含学生的学号、姓名、出生年月日
class Student
{
public:
void SetStuValues (Date &birthday)
{
cout<<"请输入学生的学号:";
cin>>num;
cout<<"请输入学生的姓名:";
cin>>name;
cout<<"请输入学生的性别(M/F):";
cin>>gender;
birthday.SetDate ();
cout<<"请输入学生的第一门课成绩:";
cin>>score1;
cout<<"请输入学生的第二门课成绩:";
cin>>score2;
cout<<"请输入学生的第三门课成绩:";
cin>>score3;
average=(score1+score2+score3)/3.0;
minscore=min({score1,score2,score3});
maxscore=max({score1,score2,score3});
cout<<"最高分为:"<<maxscore<<endl;
cout<<"最低分为:"<<minscore<<endl;
cout<<"最平均分为:"<<average<<endl;
}
void SetNum () //设置学生的学号
{
cin>>num;
}
int GetNum () //获得学生的学号
{
return num;
}
void SetName ()//设置学生的姓
{
cin>>name;
}
string GetName ()//获得学生的姓名
{
return name;
}
void Setgender()//设置学生的性别
{
    cin>>gender;
}
char Getgender()//获得学生的性别
{
    return gender;
}
void Setscore1()
{
    cin>>score1;
}
int Getscore1()
{
    return score1;
}
void Setscore2()
{
    cin>>score2;
}
int Getscore2()
{
    return score2;
}
void Setscore3()
{
    cin>>score3;
}
int Getscore3()
{
    return score3;
}
void SetBirthday (Date &birthday) //设置年月日
{
 birthday.SetDate();
}
int GetBirYear (Date &birthday) //获得年
{return birthday.GetYear();
}
int GetBirMonth (Date &birthday) //获得月
{
  return birthday.GetMonth();
}
int GetBirDay (Date &birthday) //获得日
{
return birthday.GetDay();    
}
void InputStudent (Date &birthday)
{
char choice;
cout<<"输入学生记录:"<<endl;
ofstream outfile ("D:\\DEV\\student.txt", ios::app);//打开文件
if (! outfile)
{
cout<<"文件打开失败"<<endl;
exit (0);
}
SetStuValues (birthday);
outfile <<GetNum ()<<" "<<GetName()<<" "<<Getgender()<<" "
<<birthday.GetYear() <<" "<<birthday.GetMonth ()<<" "<<birthday.GetDay ()<<" "<<
Getscore1()<<" "<<Getscore2()<<" "<<Getscore3()<<" "<<endl;
cout<<"是否继续输入学生信息输入y(继续)或者n(停止):";
cin>>choice;
if(choice=='y')
{
InputStudent (birthday);
}
outfile.close ();
system ("pause");
}
//2.浏览全部学生记录
void OutputStudent ()
{
int year;
int month;
int day;
char gender;
int score1,score2,score3;
double average;
ifstream infile ("D:\\DEV\\student.txt", ios::in);
if (! infile)
{
cout<<"文件打开失败"<<endl;
}
while (! infile.eof ())
{
infile>>num>>name>>gender>>year>>month>>day>>score1>>score2>>score3;
cout<<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<endl;
infile. get ();
if (infile.peek ()=='\n') break;
}
infile.close ();
system ("pause");
}
//3. 按学号查找学生记录
void FindByNum ()
{
int year;
int month;
int day;
char gender;
int score1,score2,score3;
double average;
int findnum;
bool find = false;
cout<<"请输入要查找学生的学号:";
cin>>findnum;
ifstream infile ("D:\\DEV\\student.txt" , ios::in);
if(! infile)
{
cout<<"文件打开失败"<<endl;
}
while (! infile.eof ())
{
infile>>num>>name>>gender>>year>>month>>day>>score1>>score2>>score3;
if (num ==findnum)
{
cout<<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<endl;
find = true;
}
infile.get ( );
if (infile.peek ()=='\n') break;
}
if(! find)
{cout<<"查无此人"<<endl;
}
infile.close ();
system ("pause");
}
//4. 按学号删除学生记录
void DeleteByNum ()
{
int year;
int month;
int day;
char gender;
int score1,score2,score3;
int findnum;
bool find = false;
cout<<"请输入要删除学生的学号:";    
cin>>findnum;
ifstream infile ("D:\\DEV\\student.txt", ios::in);
if(! infile)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("temp.txt", ios::app);
if(! outfile) //打开文件
{
cout<<"文件打开失败"<<endl;
exit (0);
}
while (! infile.eof ())
{
infile>>num>>name>>gender>>year>>month>>day>>score1>>score2>>score3;
if (num != findnum)
{
outfile <<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<endl;
}
else
{
cout<<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<endl; 
find = true;
cout<<"已经删除该学生"<<endl;
}
infile.get ();
if (infile.peek ()=='\n') break;
}
if (! find)
{
cout<<"查无此人"<<endl;
}
infile.close ();
outfile.close ();
remove ("D:\\DEV\\student.txt" ) ; //删除文件
rename ("temp.txt" , "D:\\DEV\\student.txt" ) ;
}
//5.按学号修改学生的出生日期
void AlterNameByNum ()
{
int count =0;
int year;
int month;
int day; 
int findnum;
int alteryear;
int altermonth;
int alterday;
bool find = false;
cout<<"请输入要修改学生的学号";
cin>>findnum;
cout<<"请输入要修改学生的出生日期";
cin>>alteryear>>altermonth>>alterday;
ifstream infile ("D:\\DEV\\student.txt", ios::in);
if(!infile)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("temp.txt" , ios::app) ;
if (! outfile)
{
cout<<"文件打开失败"<<endl;
exit (0);
}
while (! infile.eof ())
{
infile>>num>>name>>gender>>year>>month>>day>>score1>>score2>>score3;
if(num!=findnum)
{
outfile <<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<endl;
}
else
{ 
outfile <<num<<" "<<name<<" "<<gender<<" "<<alteryear<<" "<<altermonth<<" "<<alterday<<" "<<score1<<" "<<
score2<<" "<<score3<<" "<<endl;
 find = true;
cout<<"已经修改该学生"<<endl;
}
infile.get ();
if (infile.peek () =='\n') break;
}
if(! find)
{
cout<<"查无此人"<<endl;
}
infile.close ();
outfile.close ();
remove (" D:\\DEV\\student.txt" ) ; //删除文件
rename ("temp.txt", "D:\\DEV\\student.txt");
}
//6.按学号修改学生的出生日期
void AlterScoreByNum ()
{
int count =0;
int year;
int month;
int day; 
int findnum;
int newScore1;
int newScore2;
int newScore3;
bool find = false;
cout<<"请输入要修改学生的学号";
cin>>findnum;
cout<<"请输入要修改学生的成绩";
cin>>newScore1>>newScore2>>newScore3;
ifstream infile ("D:\\DEV\\student.txt", ios::in);
if(!infile)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("temp.txt" , ios::app) ;
if (! outfile)
{
cout<<"文件打开失败"<<endl;
exit (0);
}
while (! infile.eof ())
{
infile>>num>>name>>gender>>year>>month>>day>>score1>>score2>>score3;
if(num!=findnum)
{
outfile <<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<endl;
}
else
{ 
outfile <<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
newScore1<<" "<<newScore2<<" "<<newScore3<<" "<<endl;
 find = true;
cout<<"已经修改该学生"<<endl;
}
infile.get ();
if (infile.peek () =='\n') break;
}
if(! find)
{
cout<<"查无此人"<<endl;
}
infile.close ();
outfile.close ();
remove (" D:\\DEV\\student.txt" ) ; //删除文件
rename ("temp.txt", "D:\\DEV\\student.txt");
}
private:
int num;
string  name;
Date birthday; 
int minscore;
int maxscore;
double average;
char gender;
int score1,score2,score3;
};
void MainShow ()
{
cout<<"\t\t ***学生信息系统***"<<endl;
cout<<"\t\t ***1.输入学生记录**"<<endl;
cout<<"\t\t ***2.浏览全部学生记录***"<<endl;
cout<<"\t\t ***3.按学号查找学生记录**"<<endl;
cout<<"\t\t ***4.按学号删除学生记录***"<<endl;
cout<<"\t\t ***5.按学号修改学生的出生日期*** "<<endl;
cout<<"\t\t ***6.按学号修改学生的成绩*** "<<endl;
cout<<"\t\t ***7.退出系统***"<<endl;
}
void Select()
{
int num;
Student stu;
Date birthday;
cout<<"请选择功能:";
cin>>num;
switch(num)
{
case 1:
stu.InputStudent (birthday);
Select ();
break;
case 2:
stu.OutputStudent ();
Select ();
break;
case 3:
stu.FindByNum ();
Select ();
break;
case 4: 
stu.DeleteByNum ();
Select ();
break;
case 5:
stu.AlterNameByNum();
Select ();
break;
case 6:
stu.AlterScoreByNum();
Select ();
break;
case 7:
cout<<"系统结束,谢谢再见"<<endl;
exit (0);
break;
}
}
int main ()
{
MainShow ();
Select ();
return 0;
}

输入为这样

img


temp文件里面的内容为这样

img


如果我只想让temp文件里面的内只有一行,并且里面的出生日期和成绩是修改后的,需要怎么修改

  • 写回答

3条回答 默认 最新

  • 阿里嘎多学长 2024-06-15 23:09
    关注

    以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:


    要实现这个功能,你需要对现有的C++代码进行一些修改。以下是修改的步骤和建议:

    1. 修改AlterNameByNumAlterScoreByNum函数:确保这两个函数在修改学生信息后,只将修改后的信息写入到临时文件中。

    2. 删除原有记录:在写入修改后的信息之前,先删除原有文件中的记录。

    3. 只保留修改后的记录:在修改学生信息后,只将这一行写入到临时文件中。

    4. 更新原文件:使用rename函数将临时文件重命名为原文件名,以覆盖原有文件。

    下面是修改后的AlterNameByNumAlterScoreByNum函数的示例代码:

    // 修改学生的出生日期
    void AlterNameByNum() {
        int findnum;
        int alteryear, altermonth, alterday;
        cout << "请输入要修改学生的学号: ";
        cin >> findnum;
        cout << "请输入要修改学生的出生日期: ";
        cin >> alteryear >> altermonth >> alterday;
    
        ifstream infile("D:\\DEV\\student.txt", ios::in);
        ofstream outfile("temp.txt", ios::out); // 使用 ios::out 来覆盖内容
    
        if (!infile) {
            cout << "文件打开失败" << endl;
            exit(0);
        }
    
        bool find = false;
        while (!infile.eof()) {
            infile >> num >> name >> gender >> year >> month >> day >> score1 >> score2 >> score3;
            if (num == findnum) {
                // 写入修改后的信息
                outfile << num << " " << name << " " << gender << " "
                        << alteryear << " " << altermonth << " " << alterday << " "
                        << score1 << " " << score2 << " " << score3 << endl;
                find = true;
                cout << "已经修改该学生" << endl;
            } else {
                // 如果不是要修改的记录,也写入临时文件,以便稍后复制回原文件
                outfile << num << " " << name << " " << gender << " "
                        << year << " " << month << " " << day << " "
                        << score1 << " " << score2 << " " << score3 << endl;
            }
        }
        if (!find) {
            cout << "查无此人" << endl;
        }
    
        infile.close();
        outfile.close();
        remove("D:\\DEV\\student.txt"); // 删除原文件
        rename("temp.txt", "D:\\DEV\\student.txt"); // 重命名临时文件为原文件名
    }
    
    // 修改学生的成绩,代码逻辑与上面类似,只需修改写入成绩的部分即可。
    

    请注意,上面的代码只是一个示例,你需要根据你的具体需求和代码结构进行调整。

    至于参考资料,你可以查看C++标准库中关于文件操作的部分,特别是<fstream>库的使用。这里有一个在线的C++标准库参考:cplusplus.com

    另外,Stack Overflow是一个很好的社区,你可以在那里找到很多关于C++编程的问题和解答:Stack Overflow

    希望这些修改能帮助你解决问题。如果你有任何其他问题或需要进一步的帮助,请随时告诉我。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月24日
  • 已采纳回答 6月16日
  • 创建了问题 6月15日

悬赏问题

  • ¥30 模拟电路 logisim
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价