void AlterNameByNum ()
{
int count =0;
int year;
int month;
int day;
int findnum;
string alteryear;
string altermonth;
string alterday;
bool find = false;
cout<<"请输入要修改学生的学号";
cin>>findnum;
cout<<"请输入要修改学生的出生日期";
cin>>alteryear>>altermonth>>alterday;
ifstream infile ("d:\\student.txt", ios::in);
if(!infile)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("studentcopy.txt" , ios::app) ;
if (! outfile)
{
cout<<"文件打开失败"<<endl;
exit (0);
}
while (! infile.eof ())
{
infile>>num>>name>>year>>month>>day;
if(num!=findnum)
{
outfile <<num<<" "<<name<<" "<<year<<" "<<month<<" "<<day<<endl;
}
else
{
outfile <<num<<" "<<alteryear<<" "<<altermonth<<" "<<alterday<<" "<<name<<endl;
find = true;
cout<<"已经修改该学生"<<endl;
}
infile.get ();
if (infile.peek () =='\n') break;
}
if(! find)
{
cout<<"查无此人"<<endl;
}
infile.close ();
outfile.close ();
remove (" d:\\student.txt" ) ; //删除文件
rename ("studentcopy.txt", "student.txt");
}
private:
int num;
string name;
Date birthday;
};
这段代码为什么不能实现修改学生的出生日期
,如果要修改应该怎么修改,写出修改后的代码
假设之前已经定义好了参数和源文件,只是在这个代码的基础上修改
关于#c++#的问题:写出修改后的代码(相关搜索:源文件)
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
关注 引自免费微信小程序:皆我百晓生
针对你提供的C++代码,确实存在一些问题导致它无法正确修改学生的出生日期。主要问题在于以下几个方面:
-
变量作用域:
count
,year
,month
,day
,findnum
,alteryear
,altermonth
,alterday
等变量在函数的局部作用域内声明,但在读取文件时却尝试直接使用,这会导致编译错误或运行时错误。 -
数据类型不匹配:
year
,month
,day
在读取时被声明为整型,但在输出到文件时,alteryear
,altermonth
,alterday
却是字符串类型,这将导致数据格式不一致。 -
输入输出格式不一致:原始文件中的数据格式与修改后输出的数据格式可能不一致,例如,原始文件中日期可能是用空格分隔的,但你尝试用多个空格分隔。
-
文件处理流程:你使用了两个文件,一个用于读取,一个用于写入,这是正确的策略,但最后删除原文件并重命名新文件时,路径有误且应该确保整个过程不出错。
下面是修改后的代码,解决了上述问题,并确保可以正确修改学生的出生日期:
#include <iostream> #include <fstream> #include <string> using namespace std; // 假设 Date 类已定义,这里只展示关键部分 class Date { public: int year, month, day; Date(int y, int m, int d) : year(y), month(m), day(d) {} friend ostream& operator<<(ostream& os, const Date& date) { return os << date.year << ' ' << date.month << ' ' << date.day; } }; // 假设 Student 类已定义,这里只展示关键部分 class Student { public: int num; string name; Date birthday; Student(int n, string nm, int y, int m, int d) : num(n), name(nm), birthday(y, m, d) {} friend ostream& operator<<(ostream& os, const Student& s) { return os << s.num << ' ' << s.name << ' ' << s.birthday; } }; void AlterNameByNum() { int findnum; int alteryear, altermonth, alterday; cout << "请输入要修改学生的学号: "; cin >> findnum; cout << "请输入要修改学生的出生日期(年 月 日): "; cin >> alteryear >> altermonth >> alterday; ifstream infile("d:\\student.txt", ios::in); ofstream outfile("studentcopy.txt", ios::out); if (!infile || !outfile) { cout << "文件打开失败" << endl; exit(0); } bool find = false; while (!infile.eof()) { int num; string name; int year, month, day; infile >> num >> name >> year >> month >> day; Student student(num, name, year, month, day); if (num != findnum) { outfile << student << endl; } else { Student newStudent(num, name, alteryear, altermonth, alterday); outfile << newStudent << endl; find = true; cout << "已经修改该学生" << endl; } } if (!find) { cout << "查无此人" << endl; } infile.close(); outfile.close(); remove("d:\\student.txt"); // 删除原文件 rename("studentcopy.txt", "d:\\student.txt"); // 重命名新文件 } int main() { AlterNameByNum(); return 0; }
注意:
- 确保
Student
类和Date
类已正确定义。 - 输入和输出格式应与文件中数据的格式一致。
- 文件操作应检查是否成功,并处理异常情况。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录-
悬赏问题
- ¥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 数据包 大概什么价