Help with writing to a file

Hi,

How would I go about reading an int from the last array in a binary file. A link explaining how or something similar would be fine too.

Thanks in advance


See 3rd post
Last edited on
What do you mean by "last array in a binary file"? A file is just a sequence of bytes.
The program here allows the user to enter new flight details, but I want the program to know that if the last flight entered is "flightId" = 10001 the next one would be equal to 10002

Sorry I explained that badly



Edit: I have that sorted but now I have a problem with the code. I don't think I have closed the file properly? It isn't writing to the file anymore

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
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
using namespace std;
#define MAXNAME    100
#define FILENAME   "flights.txt"

typedef struct date{
       int day;
       int month;
       int year;
       }d;

typedef struct booking{
       char bookingId[10];
       char name[20];
       d date;
       int seatBooked;
       double cost;
       }b;

typedef struct flightRecord{
       int flightId;
       char route[9];
       int maxSeats;
       int seatsAvail;
       double costPP;
       }f;
       
string GenerateCode(string , string);
void enterFlightDetails(fstream & file, f*, int);
void updateFlight(fstream & file, f*, int);
void flightReport(fstream & file, f*, int);

int main(int argc, char *argv[])
{
   fstream file;
   int size =1;
   f*fArray = new f[size];
   b*bArray = new b[size];

   int menuChoice;

   
   do{
   cout<<"Enter a choice:"<<endl;
   cout<<"1. Enter details"<<endl;
   cout<<"2. Update flights"<<endl;
   cout<<"3. Flight report"<<endl;
   cout<<"4. Exit"<<endl;
   cout<<"Enter a choice"<<endl;
   cin>>menuChoice;

   switch(menuChoice)
   {
                 case 1: cin.ignore();
                         enterFlightDetails(file,fArray,size);
                 break;
                 case 2: cin.ignore();
                         updateFlight(file,fArray,size);
                 break;
                 case 3: cin.ignore();
                         flightReport(file,fArray,size);
                 break;
                 case 4: return EXIT_SUCCESS;
                 break;
                 default: cout<<"Invalid number, please enter achoice from 1-4"<<endl;
                 break;
                 }
                 }
                 while (menuChoice > 0 || menuChoice < 5);
                 

   system("PAUSE");
   return EXIT_SUCCESS;
}

void enterFlightDetails(fstream & file, f*fArray,int size)
{     
      //allows the flight ID to be incremented by 1 each time a flight is entered
    int lastRecord;
     f record;
    file.open(FILENAME,ios::in|ios::binary);
    file.clear();
    file.seekg (0,ios:: beg);
    file.read( reinterpret_cast< char *>(&record ), sizeof(f));
    while(!file.eof()){
     lastRecord = record.flightId;
     file.read( reinterpret_cast< char *> (&record),sizeof(f));
      } 
       file.close();

char flightcodechar[8];
string departure;
string destination;
string fcode;
cout<<"Enter flight details:"<<endl;

file.open(FILENAME,ios::out|ios::app |ios::binary);

for(int i=0; i<size; i++)
        {
       cout<<"FlightID:"<<lastRecord+1<<endl;
       record.flightId = lastRecord+1;
       cout<<"Enter departure zone"<<endl;
       getline(cin,departure);
       cout<<"Enter destination zone"<<endl;
       getline(cin, destination);
       fcode = GenerateCode(departure, destination);
       for(int j=0;j<9;j++)
       flightcodechar[j]=fcode[j];
       strcpy(record.route,flightcodechar);

       cout<<"Enter the max number of seats available"<<endl;
       cin>>record.maxSeats;
       cin.ignore();
       file.write(reinterpret_cast< char *>(&record), sizeof(f));
}
file.close();
flightReport(file,fArray,size);
}

string GenerateCode(string departure, string destination)
{
      string flightcode = "";
      int i;

for(i = departure.length() - 4; i < departure.length(); i++){
      flightcode += departure[i];
      }
      
flightcode += "-";
for(i = 0; i <= 2; i++){
            flightcode += destination[i];
            }
      return flightcode;
}

void updateFlight(fstream & file, f*fArray,int size)
{
b record;
long len;

    }

void flightReport(fstream & file, f*fArray,int size)
{
cout<<"Flight Report:"<<endl;
    f record;
    file.open(FILENAME,ios::in|ios::binary);
    file.clear();
    file.seekg (0,ios:: beg);
    file.read( reinterpret_cast< char *>(&record ), sizeof(f));
    while(!file.eof())
    {
            cout <<"Flight ID:\t\t"<<record.flightId<<endl;
            cout <<"Route:\t\t"<<record.route<<endl;
            cout<<"Max seats available:\t"<<record.maxSeats<<endl;
            file.read( reinterpret_cast< char *> (&record),sizeof(f));
            } 
       file.close();
         }

Last edited on
file.clear(); was needed

Thanks if anybody took time trying to work out what on earth was going on lol
Topic archived. No new replies allowed.