File created but empty

A file is created but its empty. And when I first create an entry and then display all the entries, it does nothing. Please Help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>

using namespace std;

class comp
{
private:

    string status;
    string s;


public:
    struct tm * date;
    int comp_no;
    void get_data(int count)
    {
        comp_no = count;
        cout<<"Enter The Complaint:\n";
        getline(cin, s);

        cout<<"\nYour Complaint has been registered.\nYour Complaint number is: "<<comp_no<<"\n";
        status = "Unsolved";


    }
    void put_data()
    {
        cout<<"\n\nComplaint Number: "<<comp_no<<"\nThe status of the complaint: "<<status<<"\nThe complaint is:\n"<<s<<"\n\n\n";
    }
};

int main()
{
    comp c;
    bool ex = true;
    int ch, ch2;
    fstream inoutfile;
    int d,m,y;
    int count = 0;
    inoutfile.open("complaints.dat", ios::ate | ios::app | ios::in | ios::out | ios::binary);
    inoutfile.seekg(0,ios::beg);

    while(inoutfile.read((char *) &c, sizeof (c)))
    {
        count++;
    }

    while(ex)
    {
        cout<<"\nWhat would you like to do?\n"
            <<"To register a complaint, enter 1\n"
            <<"To search for a complaint, enter 2\n"
            <<"To update the status of a complaint to solved, enter 3\n"
            <<"To exit, enter 0\n";
        cin>>ch;
        switch (ch)
        {
        case 0:
        {
            ex = false;
            break;
        }
        case 1:
        {
            cin.ignore();
            inoutfile.seekg(0, ios::end);
            inoutfile.seekp(0,ios::end);
            count++;
            c.get_data(count);
            inoutfile.write((char *) &c, sizeof (c));

            break;
        }
        case 2:
        {
            inoutfile.seekg(0,ios::beg);

            while(inoutfile.read((char *) &c, sizeof (c)))
            {
                    c.put_data();
             }
            break;
        }
        case 3:
        {
            cout<<"\nEnter the complaint number whose status is to be updated: ";
            cin>>d;
            inoutfile.seekp(0,ios::beg);
            inoutfile.seekg(0,ios::beg);

            char flag;
            fstream temp;
            temp.open("temp.dat", ios::ate | ios::in | ios::out | ios::binary);
            while(inoutfile.read((char *) &c, sizeof (c)))
            {
                if(c.comp_no == d )
                {
                    flag='y';
                    cout<<"\nComplaint Status is now updated to Solved\n";
                    c.put_data();
                }
                if(!inoutfile.eof())
                {
                    temp.write((char*) &c, sizeof (c));
                }
                else
                    break;
            }
            if(flag!='y')
                cout<<"\n sorry no such record present ";
            inoutfile.close();
            temp.close();
            remove("complaints.dat");
            rename("temp.dat" ,"complaints.dat");
            inoutfile.open("complaints.dat", ios::ate | ios::in | ios::out | ios::binary);
            break;
        }

        default:
        {
            cout<<"\nEnter Correct Option\n";
            break;
        }
        }
    }

    inoutfile.close();
    return 0;
}

Last edited on
After your read loop at line 47, the fail bit will be set.
You need to call inoutfile.clear() to reset the fail bit.
All subsequent operations will be ignored while the fail bit is set.
Thanks. But now when I restart the program and if the dat file already exists then the program crashes.

And also when I run it for the first time, and add two entries and when I display it, the complaint is the same too.
Last edited on
Topic archived. No new replies allowed.