首页 > 解决方案 > 为什么不能读取文件(不显示数据)?

问题描述

*我想从用户那里得到他的地址并将其存储在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();
}

标签: c++file

解决方案


如果打开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();是不必要的,因为析构函数会自动关闭文件


推荐阅读