read data and write data in .txt file

i'm working with this code. but, it did not save the data i enter earlier after i close and re-open it

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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <iostream>
#include <conio.h>
#include <string.h>
#include <iomanip.h>
#include <fstream>
#include <process.h>

const int TRUE = 1;
const int FALSE = 0;

/************************************************************************************/

struct hotelRecord
{
	char room_no[15];
 	char name[30];
   char addr[30];
   char phone[12];
};
/************************************************************************************/
struct Linked_List
{
 struct hotelRecord hr;
 struct Linked_List *next;
};
/************************************************************************************/
typedef struct Linked_List node;

node *head = NULL,*New;
/************************************************************************************/
void initialize(node *N)
{
  cout << "\n\t\t\Room_no : ";
 cin >> N -> hr.room_no;
 cout << "\n\t\t\tName : ";
 cin >> N -> hr.name;
 cout << "\n\t\t\tAddress : ";
 cin >> N -> hr.addr;
 cout << "\n\t\t\tPhone No. : ";
 cin >> N -> hr.phone;
}
/************************************************************************************/
int empty(node *n)
{
 if(strcmp(n -> hr.room_no,"") == 0 && strcmp(n -> hr.name,"") == 0 && strcmp(n -> hr.addr,"") == 0 && strcmp(n -> hr.phone,"") == 0)
  return 1;
 else
  return 0;
}
/************************************************************************************/
void create()
{
 New = new node;
 strcpy(New -> hr.room_no,"") ;
 strcpy(New -> hr.name,"");
 strcpy(New -> hr.addr,"");
 strcpy(New -> hr.phone,"");
 New -> next = NULL;
 if(head == NULL)
  head = New;
}
/************************************************************************************/
int book_room()
{
 ofstream outfile;
 outfile.open("Recoed.dat",ios::app);
 if(outfile.fail())
  {
   cout << "Error : Couldn't open file.";
   getch();
   exit(0);
  }

 if(head == NULL)
  create();

 if(head -> next == NULL)
  if(empty(head))
     {
    initialize(New);
      outfile.write((char *) &head,sizeof(head));
      return 1;
     }

 char r_ntmp[15];
 create();
 initialize(New);
 strcpy(r_ntmp,New -> hr.room_no);
 node *temp = head,*loc = head;
 if(strcmpi(r_ntmp,head -> hr.room_no) < 0)
 {
   New -> next = head;
   head = New;
 }

 else
  {
    while(temp -> next != NULL)
     {
       temp = temp -> next;
       if(strcmpi(r_ntmp,temp -> hr.room_no) > 0)
        loc = temp;
      }

    New -> next = loc -> next;
    loc -> next = New;
   }
 return 1;
}
/************************************************************************************/
void availability(char *n)
{
 node *temp = head;
 int found = FALSE;

 if(head == NULL)
   found = FALSE;

 else if(strcmp(n,temp -> hr.room_no) == 0)
  found = TRUE;

 else
 {
  while(temp -> next != NULL)
  {
    temp = temp -> next;
    if(strcmp(n,temp -> hr.room_no) == 0)
     {
       found = TRUE;
       break;
      }
   }
  }

 if(found)
  {
     cout << "\n\n\t\t  Room No : " << temp -> hr.room_no;
    cout << "\n\n\t\t  Name : " << temp -> hr.name;
    cout << "\n\t\t  Address : " << temp -> hr.addr;
    cout << "\n\t\t  Phone NO. : " << temp -> hr.phone;
   }

 else
  {
    cout << "\n\n" << setw(44) << "Record is available";
    cout << "\n" << setw(53) << "Press any key to continue...";
   }
}

/************************************************************************************/
int remove(char *n)
{
 if(head == NULL)
    return 0;

 node *temp = head,*loc = head;
 if(strcmp(n,temp -> hr.room_no) == 0)
    {
     if(temp -> next == NULL)
       {
        head = NULL;
        delete temp;
       }

     else
       {
        head = temp -> next;
        delete temp;
       }
     return 1;
      }

 else
  {
    while(temp -> next != NULL)
     {
       temp = temp -> next;
       if(temp == NULL)
         return 0;

       if(strcmpi(n,temp ->  hr.room_no) == 0)
        {
        if(temp -> next != NULL)
          {
            temp = loc -> next;
       loc -> next = temp -> next;
       delete temp;
          }
         else
           loc -> next = NULL;

         return 1;
        }

       loc = loc -> next;
      }
  }
 return 0;
}
/************************************************************************************/
int modify(char *n)
{
 node *temp = head;
 if(head == NULL)
  return 0;

 if(strcmpi(n,temp ->  hr.room_no) == 0)
  {
    cout << "\n\n\t\t\t***** Previous Data *****";
    cout << "\n\t\t\t  Room_No : " << temp -> hr.room_no;
    cout << "\n\t\t\t  Name : " << temp -> hr.name;
    cout << "\n\t\t\t  Address : " << temp -> hr.addr;
    cout << "\n\t\t\t  Phone NO. : " << temp -> hr.phone;
    cout << "\n\n\t\t\t***** Enter New Data *****\n";
    initialize(head);
    return 1;
   }

 else
 {
  while(temp -> next != NULL)
  {
    temp = temp -> next;
    if(temp == NULL)
     return 0;

    if(strcmpi(n,temp -> hr.room_no) == 0)
     {
    cout << "\n\n\t\t\t***** Previous Data *****";
    cout << "\n\t\t\t  Room_No : " << temp -> hr.room_no;
    cout << "\n\t\t\t  Name : " << temp -> hr.name;
    cout << "\n\t\t\t  Address : " << temp -> hr.addr;
    cout << "\n\t\t\t  Phone NO. : " << temp -> hr.phone;
    cout << "\n\n\t\t\t***** Enter New Data *****\n";
    initialize(temp);
    return 1;
      }
    }
  }
 return 0;
}
/************************************************************************************/
void display()
{
 node *temp = head;
 if(head == NULL)
  {
   cout << "\n\n" << setw(46) << "Database is empty";
   cout << "\n" << setw(53) << "Press any key to continue...";
   return;
  }

 if(!empty(head))
  {
   cout << "\n\n\t\t  Room No : " << temp -> hr.room_no;
   cout << "\n\n\t\t  Name : " << temp -> hr.name;
   cout << "\n\t\t  Address : " << temp -> hr.addr;
   cout << "\n\t\t  Phone NO. : " << temp -> hr.phone << endl << endl;
  }

 while(temp -> next != NULL)
  {
    temp = temp -> next;
   cout << "\n\n\t\t  Room No : " << temp -> hr.room_no;
   cout << "\n\n\t\t  Name : " << temp -> hr.name;
   cout << "\n\t\t  Address : " << temp -> hr.addr;
   cout << "\n\t\t  Phone NO. : " << temp -> hr.phone << endl ;
   }
}
int main()
{
 char room_no[15];
  do
 {
  clrscr();
  int choice = menu();
  int success;
  cout << "\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  switch(choice)
   {
    case 1:
      success = book_room();
         if(success)
            cout << "\n\n\n" << setw(51) << "Data Inserted Successfully";

         cout << "\n" << setw(53) << "Press any key to continue...";
         break;

    case 2:
       cout << "\n\t\t\tEnter room number : ";
         cin >> room_no;
       availability(room_no);
      break;

    case 3:
      cout << "\n\t\t\tEnter Name : ";
         cin >> room_no;
         success = remove(room_no);
         if(success)
           cout << "\n\n\n" << setw(50) << "Data Deleted Successfully";
         else
           cout << "\n\n" << setw(48) << "Data Doesn't Exists";

         cout << "\n" << setw(53) << "Press any key to continue...";
      break;

    case 4:
      cout << "\n\t\t\tEnter Name : ";
         cin >> room_no;
         success = modify(room_no);
         if(success)
           cout << "\n\n\n" << setw(50) << "Data Modified Successfully";
         else
           cout << "\n\n" << setw(48) << "Data Doesn't Exists";

         cout << "\n" << setw(53) << "Press any key to continue...";
      break;

    case 5:
      display();
         break;

    case 0:
      clrscr();
         getch();
      return 0;

    default:
         cout << endl << setw(58) << "Please select appropriate option\n";
         cout << setw(55) << "Press any key to continue...";
   }

  getch();
 }while(1);
}
/***************************************************** */
Topic archived. No new replies allowed.