c++ - 在 C++ 中处理文件期间的文件写入问题
问题描述
在制作一个练习文件处理的程序时,我遇到了一个问题。在写入文件期间,我看到如下奇怪的东西,即使我将文件保存为 txt 格式并在记事本中打开。
谁能澄清一下?下面是编写代码:
int hostel::add()
{
int r,flag;
ofstream fout;
fout.open("model",ios::app);
cout<<"\n Enter Customer Details";
cout<<"\n **********************";
cout<<"\n\n Room no: ";
cin>>r;
flag=check(r);
if(flag){
cout<<"\n Sorry..!!!Room is already booked";
fout.close();
}
else
{
room_no=r;
cout<<"\n Name:\t ";
cin>>name;
cout<<"\n Address:\t ";
cin>>address;
cout<<"\n Phone No:\t ";
cin>>phone;
fout.write((char*)this,sizeof(*this));
fout.close();
cout<<"\n Your Room is booked!!";
}
cout<<"\n Press any key to continue!!";
system("pause");
return 0;
}
同样在读取文件时最后一条记录被打印 2 次:
阅读代码如下:
int hostel::rooms()
{
ifstream fin("model",ios::in);
cout<<"\n\t\t\tList Of Rooms Allotted";
cout<<"\n\n Room No.\tName\t\tAddress\t\tPhone No.\n";
while(!fin.eof())
{
fin.read((char*)this,sizeof(*this));
cout<<"\n\n"<<room_no<<"\t\t"<<name;
cout<<"\t\t"<<address<<"\t\t"<<phone;
}
cout<<"\n\n\n\t\t\tPress any key to continue!!";
system("pause");
fin.close();
return 0;
}
解决方案
推荐阅读
- r - 检查 R 中数据框列中的特定值集
- zabbix - rhel 7.2如何将zabbix 3.2升级到5.4
- javascript - 使用 React-Native-Gifted-Chat 时如何隐藏 Android 键盘?
- entity-framework-core - 告诉 ef core migrations 来获取我的自定义迁移
- flutter - 交互式查看器缩放图像不起作用
- selenium-ide - 为部分文本匹配断言文本
- html - 将 div 移动到导航栏的右侧
- python - 使用第二列的百分比变化填充列中的空值,同时按多列分组
- url - 自定义 url scipt 或 wordpress 插件
- php - 站点地图优先级动态设置 PHP