c++ - 为什么不能读取文件(不显示数据)?
问题描述
*我想从用户那里得到他的地址并将其存储在file.dat然后显示这些数据
例如:
First Name: joooooosh
Last Name: Mikeeeeee
Governorate: Gize
City: 6 October132
Area: 4th District
Street Name/NO. : Conrad, 42, Washington, D.C.
Mobile: 123456789
Building Name/No. : 833
Floor No. : 3
Apartment No. : 7
Nearest Landmark: Pharmacy*
当从文件读取显示时,不显示任何东西,为什么?
此代码是我项目的一部分,这是我的完整项目https://github.com/loai1929/Bookshop-Management-System.git
在此先感谢。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <string.h>
#include <conio.h>
using namespace std;
class Adress
{
public:
char area[50], first_name[20], last_name[20], governorate[30], city[20], street_name[100], mobile[15], building_name[20], floor_no[10], apartment_no[5], nearest_landmark[50];
void dis_ad();
void display_adress();
void get_adress();
};
void Adress::get_adress()
{
cout << "\n\n\t\t\tMy Address:-\n\n\t\t\tFirst Name: ";
cin.getline(first_name, 20);
cout << "\n\n\t\t\tLast Name: ";
cin.getline(last_name, 20);
cout << "\n\n\t\t\tGovernorate: ";
cin.getline(governorate, 30);
cout << "\n\n\t\t\tCity: ";
cin.getline(city, 20);
cout << "\n\n\t\t\tArea: ";
cin.getline(area, 50);
cout << "\n\n\t\t\tStreet Name/NO. : ";
cin.getline(street_name, 100);
cout << "\n\n\t\t\tMobile: ";
cin.getline(mobile, 15);
cout << "\n\n\n\t\t\tBuilding Name/No. : ";
cin.getline(building_name, 20);
cout << "\n\n\t\t\tFloor No. : ";
cin.getline(floor_no, 10);
cout << "\n\n\t\t\tApartment No. : ";
cin.getline(apartment_no, 5);
cout << "\n\n\t\t\tNearest Landmark: ";
cin.getline(nearest_landmark, 50);
}
void Adress::display_adress()
{
cout << "\n\n\t\t\tFirst Name: " << first_name;
cout << "\n\n\t\t\tLast Name: " << last_name;
cout << "\n\n\t\t\tGovernorate: " << governorate;
cout << "\n\n\t\t\tCity: " << city;
cout << "\n\n\t\t\tArea: " << area;
cout << "\n\n\t\t\tStreet Name/NO. : " << street_name;
cout << "\n\n\t\t\tMobile: " << mobile;
cout << "\n\n\n\t\t\tBuilding Name/No. : " << building_name;
cout << "\n\n\t\t\tFloor No. : " << floor_no;
cout << "\n\n\t\t\tFloor No. : " << apartment_no;
cout << "\n\n\t\t\tNearest Landmark: " << nearest_landmark;
}
void Adress::dis_ad()
{
ifstream iFile("shippingAddresses.dat", ios::binary);
while (iFile.read((char *)this, sizeof(*this)))
{
display_adress();
}
iFile.close();
}
int main()
{
Adress ad;
ad.dis_ad();
}
解决方案
如果打开iFile
失败,那么调用也read
将失败,while 循环的主体将永远不会执行。在使用它之前,您应该检查文件是否成功打开:
ifstream iFile("shippingAddresses.dat", ios::binary);
if (!iFile)
{
std::cout << "open failed\n";
return;
}
while (iFile.read((char *)this, sizeof(*this)))
{
display_adress();
}
请注意,这iFile.close();
是不必要的,因为析构函数会自动关闭文件
推荐阅读
- php - 如何根据当前时间从数据库中选择
- javascript - Ajax Vanilla JS - ajax 异步继续而不等待 readyState 4,状态 200
- javascript - 如何在 vue.js 中格式化以千、百万或十亿为单位的数字?
- c - 如何检查图中的循环?
- javascript - Node.js,通过对象数组进行映射,并将特定值放入新数组中
- kubernetes - 如何将一个持久卷声明的数据与另一个隔离
- mongodb - 使用来自不同集合的字段的 MongoDB 查询
- python - 按多个元素按升序和降序排列元组列表
- sql - 基于 DATE_TRUNC 的 SQL 查询中的项目计数总和
- android - 如何从firebase预构建登录ui注销