c++ - C++从文件中读取对象

标签 c++ file object file-io

我创建了一个类来存储用户的详细信息,例如帐号、姓名余额等。我正在将此类的对象写入文件。当我从文件中读取时,我没有得到正确的输出。如果输入了 2 个人的详细信息,那么我将获得第二个人的详细信息两次。这是代码。

#include <fstream>
#include <string>
#include <ctime>
# include <conio.h>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
class User
{
    int acctype[2] = {0,0}; // acctype[0] = savings account & acctype[1] = current account
    string name = "NULL";
    string address = "NULL";
    string dob = "NULL";
    long int sbal = 0;
    long int cbal = 0;
    long int accno = 0;
    string password;
    int aod = 0; // account opening date
    int aom = 0; // account opening month
    int aoy = 0; // account opening year
    public:
        void input()
        {
            system("cls");
            cin.clear();
            char type,ch;
            cout << "Enter you name\n";
            getline(cin,name);
            cout << "Enter your address\n";
            getline(cin,address);
            cout << "Enter your date of birth(dd/mm/yyyy)\n";
            getline(cin,dob);
            cout << "Enter your account type(s = savings, c = current)\n";
            cin >> type;
            if(type == 's')
            {
                acctype[0] = 1;
                cout << "Enter your savings account balance\n";
                cin >> sbal;
            }
            else if(type == 'c')
            {
                acctype[1] = 1;
                cout << "Enter your current account balance\n";
                cin >> cbal;
            }
            pass:
            cout << "\nChoose a password(8-16 characters)\n";
            // to display * in place of the entered character for the password
            ch = _getch();
            while(ch != 13)// to check for return input
            {
                password.push_back(ch);
                cout << "*";
                ch = _getch();
            }
            if(password.size() < 8 || password.size() > 16)
                goto pass;
            //store the account opening date
            time_t now = time(0);
            tm *ltm = localtime(&now);
            aod = ltm->tm_mday;
            aom = 1 + ltm->tm_mon;
            aoy = 1900 + ltm->tm_year;
            cin.ignore(10000,'\n');

        }
        void datawrite()
        {
            //store the data in the file
            ofstream fout;
            fout.open("database.txt", ios_base::app);
            if(!fout.is_open())
            {
                cout << "\nCannot open file! Aborting";
                exit(0);
            }
            fout.write((char*) this, sizeof(this));
            fout.close();
        }
        int dataread()
        {
            ifstream fin;
            fin.open("database.txt", ios_base::in);
            if(!fin.is_open())
            {
                cout << "\nCannot open file! Aborting";
                exit(0);
            }
            fin.seekg(0);
            fin.read((char*)this, sizeof(this));
            while(fin)
            {
            //system("cls");
                fin.read((char*)this, sizeof(this));
                this->accsummary();
            }
            fin.close();
        }
        void accsummary()
        {
            //system("cls");
            cout << "\n\t\tACCOUNT SUMMARY\n";
            cout << "\nName : " << name;
            cout << "\nAccount NUmber : " << accno;
            cout << "\nDate of birth : " << dob;
            cout << "\nAddress : " << address;
            cout << "\nAccount opening date : " << aod << "/" << aom << "/" << aoy;
            if(acctype[0] == 1)
                cout << "\nSavings Account Balance : " << sbal;
            if(acctype[1] == 1)
                cout << "\nCurrent Account Balance : " << cbal;

        }
};

int main()
{
    User u;
    u.input();
    u.datawrite();
    u.dataread();

}

最佳答案

这是 Why is iostream::eof inside a loop condition considered wrong? 的变体, 但测试略有不同

        while(fin)
        {
            fin.read((char*)this, sizeof(this));
            this->accsummary();
        }

在将数据添加到摘要之前,您不会测试read 是否成功。这将使循环中的最后+1 轮第二次添加最后一个数据。

while(fin) 不会终止循环,直到您有一个不成功的读取。那一轮太晚了。

关于c++ - C++从文件中读取对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32895484/

相关文章:

C++ map迭代器跳转元素

javascript - 使用 underscore.js 展平对象的 javascript 数组

c - 在c中打开一个编号的文件

perl - 使用 Perl 检测空目录

javascript - `apply` 没有 't work for function as object' 的属性

python - 这些代码行是做什么的? (运动模拟)

android - 如何将 Mat 数组从 JNI 返回到 Java

android - 使用 Opencv 从 Android 中的 CameraGLSurfaceView 拍照

c++ - 使用 boost C++ 创建 Json 数组

javascript - PHP 根据点击的 anchor 获取文件