关闭程序,然后再次运行以从文件中读取文件后,我的程序没有从文件中读取什么? [英] what is my program not reading from file after i close it and then run it again for reading from the file?

查看:116
本文介绍了关闭程序,然后再次运行以从文件中读取文件后,我的程序没有从文件中读取什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个程序,该程序以自定义数据类型存储日期和时间,然后将其存储在二进制文件中.如果我在DevC ++ 5.11(快捷键 F11 )中编译并运行该程序,那么一切都将完美运行.只要不关闭程序,我的程序就可以将数据存储在文件中,然后读取它.但是,如果我关闭程序,然后再次打开文件进行读取,它只会显示我输入的第一天,而不会显示其余部分.请告诉我该怎么做才能解决此问题.

I have written a program that stores date and time in a custom data type and then stores it in a binary file. Everything works perfectly if I compile and run the program in DevC++ 5.11 (shortcut F11). My program is able to store data in the file and then read it as long as I don't close the program. However, if I close the program and then open the file again for reading, it only shows the first day that I entered and doesn't show the rest of it. Please tell me what I need to do to fix this.

这是我的DevC ++ 5.11代码

Here is my DevC++ 5.11 code

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

char s=' ';

struct date{
    int day;
    string month;
    int year;
};

struct time{
    int hr;
    int min;
};

class entry{
    private:
        int sno;
        date d;
        time t;

    public:

        void initsno(){
            sno=0;
        }

        void getdate(){
            cout << "\nEnter the date : ";
            cout << "\nDay : ";
            cin >> d.day;
            cout << "Month : ";
            cin >> d.month;
            cout << "Yeay :";
            cin >> d.year;      
        }

        void gettime(){
            cout << "\n\nEnter time : ";
            cout << "\nHours :";
            cin >> t.hr;
            cout << "Minutes :";
            cin >> t.min;
        }   

        void showdate(){
            cout << "\n\nDate is : ";
            cout << d.day << s << d.month << s << d.year << endl << endl;
        }

        void showtime(){
            cout << "\n\nTime is :";
            cout << t.hr << ":" << t.min << endl << endl;
        }           
};

int main(){

    //declaring variables
    char c='y', l;
    string filename;
    entry e, e2;
    ofstream filew;
    ifstream filer; 

    cout << "******Time sheet calculator******" << endl << endl;

    //getting file name begins
    cout << "Enter the name of file you want to create : ";
    cin >> filename;
    cout << "\nThe file name is : " << filename << endl;
    //getting file name over

    e.initsno();    //initializing the entry serial number

    //creating and opening file
    filew.open(filename.c_str(), ios::binary | ios::out | ios::app);
    //file association operation successful

    if (!filew)
        cout << "\nCan't open file" << endl;
    else
        cout << "continue write operation\n";

    //getting date
    e.getdate();

    cout << "\nDo you want to write to file? :";
    cin >> l;

    if (l == 'y')
    {
        //Entering time
        while (c == 'y')
        {
            e.gettime();
            filew.write((char*)&e, sizeof(e));
            cout << "\nFile write operation successful.\n";
            cout << "\nDo you wish to continue? :";
            cin >> c;
        }
    }
    else
        cout << "\nContinue reading...\n";

    //closing file
    filew.close();

    cout << "\nReading file...\n";

    //opening file for reading
    filer.open(filename.c_str(), ios::binary | ios::in);

    if (!filer)
        cout << "\nCan't open file" << endl;
    else
        cout << "continue read operation\n";

    while (filer.read((char*)&e2, sizeof(e2)))
    {
        cout << "\nDisplaying file data...\n";
        e2.showdate();
        e2.showtime();
    }

    cout << "\n\nFile IO successful...";

    filer.close();

    return 0;
}

推荐答案

您的date结构包含string成员.您不能按原样write()/read()string,因为字符数据(通常是 1 )存储在内存中的其他地方.

Your date struct contains a string member. You can't write()/read() a string as-is, since the character data is (usually1) stored elsewhere in memory.

1:不考虑短字符串优化,在这种情况下,少量字符数据实际上直接存储在string对象中.

1: not taking Short String Optimization into account, in which case small amounts of character data are actually stored directly in the string object.

使用固定的char[]数组而不是string,例如:

Use a fixed char[] array instead of a string, eg:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct date{
    int day;
    char month[10];
    int year;
};

struct time{
    int hr;
    int min;
};

class entry{
    private:
        int sno;
        date d;
        time t;

    public:

        void initsno(){
            sno=0;
        }

        void getdate(){
            cout << "\nEnter the date : ";
            cout << "\nDay : ";
            cin >> d.day;
            cout << "Month : ";
            cin.get(d.month, sizeof(d.month));
            cout << "Year :";
            cin >> d.year;      
        }

        void gettime(){
            cout << "\n\nEnter time : ";
            cout << "\nHours :";
            cin >> t.hr;
            cout << "Minutes :";
            cin >> t.min;
        }   

        void showdate(){
            cout << "\n\nDate is : ";
            cout << d.day << s << d.month << s << d.year << endl << endl;
        }

        void showtime(){
            cout << "\n\nTime is :";
            cout << t.hr << ":" << t.min << endl << endl;
        }           
};

int main(){

    //declaring variables
    char c;
    string filename;
    entry e, e2;
    ofstream filew;
    ifstream filer; 

    cout << "******Time sheet calculator******" << endl << endl;

    //getting file name begins
    cout << "Enter the name of file you want to create : ";
    cin >> filename;
    cout << "\nThe file name is : " << filename << endl;
    //getting file name over

    e.initsno();    //initializing the entry serial number

    //creating and opening file
    filew.open(filename.c_str(), ios::binary | ios::app);
    //file association operation successful

    if (!filew)
        cout << "\nCan't open file" << endl;
    else
        cout << "continue write operation\n";

    //getting date
    e.getdate();

    cout << "\nDo you want to write to file? :";
    cin >> c;

    if (c == 'y')
    {
        //Entering time
        do
        {
            e.gettime();
            filew.write((char*)&e, sizeof(e));
            cout << "\nFile write operation successful.\n";
            cout << "\nDo you wish to continue? :";
            cin >> c;
        }
        while (c == 'y');
    }
    else
        cout << "\nContinue reading...\n";

    //closing file
    filew.close();

    cout << "\nReading file...\n";

    //opening file for reading
    filer.open(filename.c_str(), ios::binary);

    if (!filer)
        cout << "\nCan't open file" << endl;
    else
        cout << "continue read operation\n";

    while (filer.read((char*)&e2, sizeof(e2)))
    {
        cout << "\nDisplaying file data...\n";
        e2.showdate();
        e2.showtime();
    }

    cout << "\n\nFile IO successful...";

    filer.close();

    return 0;
}

否则,您需要序列化 string(即保存其size()值和其实际字符数据),例如:

Otherwise, you need to serialize the string (ie, save its size() value followed by its actual character data), eg:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct date{
    int day;
    string month;
    int year;

    void read(istream &is) {
        is.read(reinterpret_cast<char*>(&day), sizeof(day));
        size_t size;
        if (is.read(reinterpret_cast<char*>(&size), sizeof(size)))
        {
            month.resize(size);
            is.read(&month[0], size);
        }
        is.read(reinterpret_cast<char*>(&year), sizeof(year));
    }

    void write(ostream &os) const {
        os.write(reinterpret_cast<const char*>(&day), sizeof(day));
        size_t size = month.size();
        os.write(reinterpret_cast<const char*>(&size), sizeof(size));
        os.write(month.c_str(), size);
        os.write(reinterpret_cast<const char*>(&year), sizeof(year));
    }
};

istream& operator>>(istream &is, date &d)
{
    d.read(is);
    return is;
}

ostream& operator<<(ostream &os, const date &d)
{
    d.write(os);
    return os;
}

struct time{
    int hr;
    int min;

    void read(istream &is)
    {
        is.read(reinterpret_cast<char*>(&hr), sizeof(hr));
        is.read(reinterpret_cast<char*>(&min), sizeof(min));
    }

    void write(ostream &os) const
    {
        os.write(reinterpret_cast<const char*>(&hr), sizeof(hr));
        os.write(reinterpret_cast<const char*>(&min), sizeof(min));
    }
};

istream& operator>>(istream &is, time &t)
{
    t.read(is);
    return is;
}

ostream& operator<<(ostream &os, const time &t)
{
    t.write(os);
    return os;
}

class entry{
    private:
        int sno;
        date d;
        time t;

    public:

        void initsno(){
            sno=0;
        }

        void getdate(){
            cout << "\nEnter the date : ";
            cout << "\nDay : ";
            cin >> d.day;
            cout << "Month : ";
            cin >> d.month;
            cout << "Yeay :";
            cin >> d.year;      
        }

        void gettime(){
            cout << "\n\nEnter time : ";
            cout << "\nHours :";
            cin >> t.hr;
            cout << "Minutes :";
            cin >> t.min;
        }   

        void showdate(){
            cout << "\n\nDate is : ";
            cout << d.day << s << d.month << s << d.year << endl << endl;
        }

        void showtime(){
            cout << "\n\nTime is :";
            cout << t.hr << ":" << t.min << endl << endl;
        }

        void read(istream &is) {
            is.read(reinterpret_cast<char*>(&sno), sizeof(sno));
            d.read(is);
            t.read(is);
        }

        void write(ostream &os) const {
            os.write(reinterpret_cast<const char*>(&sno), sizeof(sno));
            d.write(os);
            t.write(os);
        }
};

istream& operator>>(istream &is, entry &e)
{
    e.read(is);
    return is;
}

ostream& operator<<(ostream &os, const entry &e)
{
    e.write(os);
    return os;
}

int main(){

    //declaring variables
    char c;
    string filename;
    entry e, e2;
    ofstream filew;
    ifstream filer; 

    cout << "******Time sheet calculator******" << endl << endl;

    //getting file name begins
    cout << "Enter the name of file you want to create : ";
    cin >> filename;
    cout << "\nThe file name is : " << filename << endl;
    //getting file name over

    e.initsno();    //initializing the entry serial number

    //creating and opening file
    filew.open(filename.c_str(), ios::binary | ios::app);
    //file association operation successful

    if (!filew)
        cout << "\nCan't open file" << endl;
    else
        cout << "continue write operation\n";

    //getting date
    e.getdate();

    cout << "\nDo you want to write to file? :";
    cin >> c;

    if (c == 'y')
    {
        //Entering time
        do
        {
            e.gettime();
            filew << e;
            cout << "\nFile write operation successful.\n";
            cout << "\nDo you wish to continue? :";
            cin >> c;
        }
        while (c == 'y');
    }
    else
        cout << "\nContinue reading...\n";

    //closing file
    filew.close();

    cout << "\nReading file...\n";

    //opening file for reading
    filer.open(filename.c_str(), ios::binary);

    if (!filer)
        cout << "\nCan't open file" << endl;
    else
        cout << "continue read operation\n";

    while (filer >> e2)
    {
        cout << "\nDisplaying file data...\n";
        e2.showdate();
        e2.showtime();
    }

    cout << "\n\nFile IO successful...";

    filer.close();

    return 0;
}

这篇关于关闭程序,然后再次运行以从文件中读取文件后,我的程序没有从文件中读取什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆