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条回答 默认 最新
关注 引自免费微信小程序:皆我百晓生
针对你提供的代码,有几个关键点需要注意以便正确地修改学生的出生日期。首先,代码中的
outfile
写入数据时,直接将num
、alteryear
、altermonth
、alterday
和name
作为字符串输出,而没有考虑它们的数据类型。此外,AlterNameByNum
函数中对student.txt
文件的读取和studentcopy.txt
的写入逻辑存在一些错误。以下是修改后的代码,以确保可以正确地读取和修改学生的出生日期:#include <iostream> #include <fstream> #include <string> #include <cstdlib> // for remove and rename #include <ctime> // for strftime using namespace std; // Assuming Date is a class that holds the date information class Date { public: int year, month, day; Date(int y, int m, int d) : year(y), month(m), day(d) {} string toString() const { char buffer[11]; strftime(buffer, sizeof(buffer), "%Y-%m-%d", localtime(&(mktime(&(struct tm){0, 0, 0, day, month - 1, year - 1900})))); // Convert to string return string(buffer); } }; void AlterNameByNum() { int findnum; int alteryear, altermonth, alterday; cout << "请输入要修改学生的学号: "; cin >> findnum; cout << "请输入要修改学生的出生日期(格式:年 月 日): "; cin >> alteryear >> altermonth >> alterday; ifstream infile("d:\\student.txt", ios::in); if (!infile) { cout << "文件打开失败" << endl; return; } ofstream outfile("studentcopy.txt", ios::out); // Use 'out' instead of 'app' to overwrite the file if (!outfile) { cout << "文件打开失败" << endl; exit(0); } int num; string name; Date birthday, new_birthday(alteryear, altermonth, alterday); bool found = false; while (infile >> num >> name >> birthday.year >> birthday.month >> birthday.day) { if (num != findnum) { outfile << num << " " << name << " " << birthday.toString() << endl; } else { outfile << num << " " << name << " " << new_birthday.toString() << endl; found = true; cout << "已经修改该学生信息" << endl; } } if (!found) { cout << "查无此人" << endl; } infile.close(); outfile.close(); if (remove("d:\\student.txt") == 0) { // Check if the removal was successful if (rename("studentcopy.txt", "d:\\student.txt") != 0) { cout << "重命名文件失败" << endl; } } else { cout << "删除文件失败" << endl; } } int main() { AlterNameByNum(); return 0; }
关键更改点:
- 使用
Date
类来存储和处理日期。 - 将
outfile
的模式从ios::app
更改为ios::out
,以确保覆盖而不是追加到文件。 - 在
AlterNameByNum
函数中,使用toString()
方法将日期转换为字符串进行输出。 - 检查文件操作是否成功,并在适当的位置添加错误处理。
以上修改后的代码应该可以正确读取并修改学生的出生日期。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决评论 打赏 举报 编辑记录无用 1- 使用
悬赏问题
- ¥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 数据包 大概什么价