Program for file is crashing

Program is crashing, if the DAT file exists. 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
136
137
138
139
140
  #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++;
    }
    inoutfile.clear();

    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.clear();
            inoutfile.seekg(0,ios::beg);
            inoutfile.seekp(0,ios::beg);

            while(inoutfile.read((char *) &c, sizeof (c)))
            {

                    c.put_data();
                //else
                  //  cout<<"\nNo Record Found\n";
            }
            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
Are you sure that complaints.dat exist in the right directory? It's a good practice to put a guard after opening a file for checking his existence... Maybe a inoutfile.good() or a inoutfile.is_open() will help you to check if the file exist in the proper directory...
what compiler are you using?? i dont think the complaints are getting stored properly in your respective file , option 3 of your program does not work. check your logic again
I am using CodeBlocks. But the problems isn't there. A file is created in the same directory as the program if I delete the DAT file its created again there and the program runs but if I close the program and run it again it doesn't. And even when I run it for the first time, then also if I first add two entries and then list all of them it displays them with same string.

And yeah I know it isn't working but the Option 2 isn't working either.
Last edited on
vishesh92 can you explain the need for that pointer to structure tm you have declared called date?
It ain't required. I was earlier thinking of adding the date.
Had any luck as of yet?
ne555 is right , the string manipulations are the reason why your program is crashing , vishesh use a buffer of enough length , thats probably one way
Thanks everyone, it worked. I used char arrays instead of strings.
Topic archived. No new replies allowed.