Writing in a random access file

I'm trying to create a program that writes in a random access file and I'm having trouble getting it done.

Can you please tell me what's wrong with my code? I think there's something wrong in the class part. Thanks.

Here's my code:

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;


class hardwareData
{
public:
//default hardwareData constructor
hardwareData :: hardwareData (string nameOfToolsValue, int qtyOfToolsValue,
double costOfToolsValue, int recNumValue);

string getNameOfTools()
{
return nameOfTools;
}//end funtion getNameOfTools

void setNameOfTools (string nameOfToolsValue)
{
nameOfTools = nameOfToolsValue;
}//end function setNameOfTools

int getQtyOfTools()
{
return qtyOfTools;
}//end funtion getQtyOfTools

void setQtyOfTools (int qtyOfToolsValue)
{
qtyOfTools = qtyOfToolsValue;
}//end of function setQtyOfTools

double getCostOfTools()
{
return costOfTools;
}//end function getCostOfTools

void setCostOfTools (double costOfToolsValue)
{
costOfTools = costOfToolsValue;
}//end function setCostOfTools

int getRecNum()
{
return recNum;
}//end function getRecNum

void setRecNum (int recNumValue)
{
recNum = recNumValue;
}//end function setRecNum


private:
string nameOfTools;
int qtyOfTools;
double costOfTools;
int recNum;
};//end class hardwareData


int main ()
{
string nameOfTools;
int qtyOfTools;
double costOfTools;
int recNum;

ofstream outHardware("hardware.dat", ios::out);

//exit program if ofstream could not open file
if (!outHardware)
{
cerr << "File could not be opened." << endl;
exit (1);
}//end if

hardwareData hardware;


cout << "Enter Record Number. (Enter 0 to end input.)" << endl;
cin >> recNum;
cout << endl;



while (recNum > 0 && recNum <=100)
{
cout << "Tool Name" << setw(15) << "Quantity" << setw(10)
<< "Cost/Unit" << endl;

cin >> nameOfTools >> setw(14) >> qtyOfTools
>> setw(5) >> costOfTools;
cout << endl << endl;

//setting the values of tool names, quantity, cost, and record number
hardware.setNameOfTools (nameOfTools);
hardware.setQtyOfTools (qtyOfTools);
hardware.setCostOfTools (costOfTools);

//seek position in file of user-specified record
outHardware.seekp ( (hardware.getRecNum() -1) * sizeof (hardware));

//write user-specified information in file
outHardware.write (reinterpret_cast < const char * > (&hardware),
sizeof (hardwareData) );

//enables the user to enter another item.
cout << "Enter Record Number. (Enter 0 to end input.)" << endl << endl;
}//end while


return 0;
}//end funtion main
closed account (1yvXoG1T)
So unless I completely missed what you were aiming for then I got your problem fixed
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
#include <iostream>
#include <string>
#include <limits>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;


class hardwareData
{
public:

    string getNameOfTools()
    {
        return nameOfTools;
    }//end funtion getNameOfTools

    void setNameOfTools (string nameOfToolsValue)
    {
        nameOfTools = nameOfToolsValue;
    }//end function setNameOfTools

    int getQtyOfTools()
    {
        return qtyOfTools;
    }//end funtion getQtyOfTools

    void setQtyOfTools (int qtyOfToolsValue)
    {
        qtyOfTools = qtyOfToolsValue;
    }//end of function setQtyOfTools

    double getCostOfTools()
    {
        return costOfTools;
    }//end function getCostOfTools

    void setCostOfTools (double costOfToolsValue)
    {
        costOfTools = costOfToolsValue;
    }//end function setCostOfTools

    int getRecNum()
    {
        return recNum;
    }//end function getRecNum

    void setRecNum (int recNumValue)
    {
        recNum = recNumValue;
    }//end function setRecNum


private:
    string nameOfTools;
    int qtyOfTools;
    double costOfTools;
    int recNum;
};//end class hardwareData


int main ()
{
    string nameOfTools;
    int qtyOfTools;
    double costOfTools;
    int recNum;
    int recsize = sizeof(string) + sizeof(int) + sizeof(double) + sizeof(int);

    fstream outHardware("hardware.dat", ios::in | ios::out);

//exit program if ofstream could not open file
    if (!outHardware)
    {
        cerr << "File could not be opened." << endl;
        exit (1);
    }//end if

    hardwareData hardware;


    cout << "Enter Record Number. (Enter 0 to end input.)" << endl;
    cin >> recNum;
    cout << endl;



    while (recNum > 0 && recNum <=100)
    {
        hardware.setRecNum(recNum);

        cout << "Tool Name" << setw(15) << "Quantity" << setw(10)
             << "Cost/Unit" << endl;

        cin >> nameOfTools >> setw(14) >> qtyOfTools
            >> setw(5) >> costOfTools;
        cout << endl << endl;

//setting the values of tool names, quantity, cost, and record number
        hardware.setNameOfTools (nameOfTools);
        hardware.setQtyOfTools (qtyOfTools);
        hardware.setCostOfTools (costOfTools);

//seek position in file of user-specified record
        outHardware.seekp ( (hardware.getRecNum() -1) * sizeof (hardware));

//write user-specified information in file
        outHardware.write (reinterpret_cast<char*> (&nameOfTools),
                           sizeof (string) );
        outHardware.write (reinterpret_cast<char*> (&qtyOfTools),
                           sizeof (int) );
        outHardware.write (reinterpret_cast<char*> (&costOfTools),
                           sizeof (double) );

//enables the user to enter another item.
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Enter Record Number. (Enter 0 to end input.)" << endl;
        cin >> recNum;
    }//end while

    outHardware.close();


    return 0;
}//end funtion main


Here's the problems I found:
1. You were declaring a constructor that accepted 4 values but never defined what the constructor did with those and then just set the values inside the main body of the code anyways.
2. You never defined the size of a record, nor applied where a record would be so it never knew where to write and where not to. The first time I ran it I got a hardware.dat that was 180MB o.O;
3. After the first run, you didn't ask for a new record number so it was stuck in the loop of writing in the same place and never letting you exit the program.
4. Didn't close the file at the end. Technically, you don't have to but I HIGHLY suggest it.

Hope this was useful :D
Thank you soo much! this has been a great help for me:)
Topic archived. No new replies allowed.