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

关于#c++#的问题:为什么文件打开失败,而且每一次运行时生成的文件里面的内容是错误的


//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:\\student.txt", ios::in);
if(!infile)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("copy.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:\\student.txt" ) ; //删除文件
rename ("copy.txt", "student.txt");
}
//6.按学号修改学生的出生日期
void AlterScoreByNum ()
{
int count =0;
int year;
int month;
int day; 
int findnum;
int score11;
int score22;
int score33;
bool find = false;
cout<<"请输入要修改学生的学号";
cin>>findnum;
cout<<"请输入要修改学生的成绩";
cin>>score11>>score22>>score33;
ifstream infile ("d:\\student.txt", ios::in);
if(!infile)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("copy.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<<" "<<score11<<" "<<score22<<" "<<score33<<" "<<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 ("copy.txt", "student.txt");
}
private:
int num;
string  name;
Date birthday; 
int minscore;
int maxscore;
double average;
char gender;
int score1,score2,score3;
};

img


为什么会显示这样的结果,为什么文件打开失败,而且每一次运行时生成的文件里面的内容是错误的

  • 写回答

3条回答 默认 最新

  • 阿里嘎多学长 2024-06-15 21:30
    关注

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


    根据您提供的代码和描述,这里有几个可能的原因导致文件打开失败以及生成的文件内容错误:

    1. 文件路径问题:您的代码中使用了硬编码的文件路径 "d:\\student.txt"。如果这个路径在您的系统中不存在或者程序没有访问该路径的权限,就会导致文件打开失败。

    2. 文件访问权限:确保程序运行的用户具有读取和写入指定文件的权限。

    3. 文件存在性:如果 "d:\\student.txt" 文件不存在,程序应该能够创建它。但是,在您的代码中,并没有检查文件是否存在的逻辑。

    4. 代码逻辑错误

      • 您的代码中使用了 infile.get(); 来读取换行符,但是紧接着的 if (infile.peek () =='\n') break; 可能不会按预期工作,因为 peek 会读取下一个字符,而 get 已经读取了当前字符。
      • 变量 find 被用于标记是否找到要修改的学生记录,但是 find 变量在 while 循环之外没有重置,这可能导致在处理多个学生记录时出现逻辑错误。
    5. 文件打开和关闭:您的代码在文件操作结束后正确地关闭了文件,但是请确保在所有可能的退出点都关闭文件,包括在 if(!infile)if(!outfile) 之后的 exit(0); 调用之前。

    6. removerename 函数的使用:您的代码在最后使用 remove 删除原始文件,然后使用 rename 重命名临时文件。请确保这些操作是您想要的,并且 copy.txt 文件在重命名之前是完整的。

    7. 截图信息分析:截图显示了程序运行时的输入输出交互过程,但是没有提供足够的信息来确定问题的具体原因。不过,它表明程序在尝试查找和修改学生记录时遇到了问题。

    8. 其他潜在问题

      • 您的代码中使用了 Date 类型,但是没有提供这个类的实现。如果 Date 类型没有正确地重载输入输出操作符,那么在读写日期时可能会出现问题。
      • 您的代码片段中有一些变量如 num, name, gender, score1, score2, score3 等在函数外部定义,但在函数内部使用,这可能会导致未定义行为。

    为了解决这些问题,您需要:

    • 确保文件路径正确,并且程序有权限访问该路径。
    • 在代码中添加逻辑来检查文件是否存在,如果不存在则创建它。
    • 检查和修复代码中的逻辑错误,确保变量在正确的作用域内使用。
    • 确保在所有退出点都关闭文件,避免资源泄露。
    • 审查 removerename 的使用,确保它们按预期工作。

    如果您需要更详细的帮助,可以提供完整的类定义和更多的上下文信息。同时,确保您的开发环境设置正确,并且您有权限访问相关的文件系统资源。

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

报告相同问题?

问题事件

  • 系统已结题 6月23日
  • 已采纳回答 6月15日
  • 创建了问题 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 数据包 大概什么价