Direct Access Files

I am extremely new to C++ (and programming in general really) and am working on a project that has me stumped (not hard to do ha). The project involves Direct Access Files. We are to create a file consisting of a series of parts records. Here are some of the specifications:

1
2
3
4
5
6
    Should contain a header record (24 bytes - filled) indicating the number of valid items.
    Each (24 byte-long) data record will contain a stock number (4 digits max), a description (8 characters max), a count (4 digits), and a "test part" indicator( 4 digits max => -1 - end of file).
    This file will initially hold 20 blank (dummy) records and will be created sequentially.
    Once the file has been created, an update sequential text file will be accessed based on stock numbers and new records will be inserted into the file.
    When the update is complete, the valid parts records will be printed in order starting at stock record "1".
    Update by reading the text update file (prog4.dat) and seeking the file position based on the stock number (don't forget the header record) 


Here is an example:

1
2
3
4
5
6
7
8
9
10
11
Initial (empty)
    Input (updates)
        1 widgits 25 3
        6 gidgits 12 8
        8 kidgits 6 -1
        3 didgits 11 6
    Output
        1 widgits 25
        3 didgits 11
        6 gidgits 12
        8 kidgits 6


I know absolutely nothing about Direct Access files, so have been looking at a couple different links that I've found on Google (http://cee-ux49.cee.illinois.edu/cee490/public_html/pdfs_vgs/aL23_Direct_Access_Files.pdf , and http://www.learncpp.com/cpp-tutorial/137-random-file-io/ specifically), but am having trouble figuring out how to make this work for this particular program.

I haven't done much in the way of code since like I said, this has me stumped, and what I do have is based primarily off of that first link so I don't know that it's right (not sure what size to give vector since I'm not entirely sure what that would be in my specific problem (the array of parts perhaps?)), but here is what little I've been able to come up with.

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
#include <iostream>
    #include <string>
    #include <fstream>
    #include <stream>
    using namespace std;
    
    class records {
    public:
            int getStckNumber() {
                    return stockNumber;
            }
            void setStockNumber(int stockNum) {
                    stockNumber = stockNum;
            }
    
            string getItemDespcription() {
                    return itemDescription;
            }
            void setItemDespcription(string itemDescrip) {
                    itemDescription = itemDescrip;
            }
    
            int getItemAmount() {
                    return itemAmount;
            }
            void setItemAmount(int itemAmt) {
                     itemAmount = itemAmt;
            }
    
            int getNext() {
                    return next;
            }
            void setNext(int nxt) {
                    next = nxt;
            }
    private:
            int stockNumber;
            string itemDescription;
            int itemAmount;
            int next;
            int recNum;
    }
    
    
    int main() {
            int stockNumber;
            string itemDescription;
            int itemAmount;
            int next;
            int recNum;
            int recSize = sizeof(int) + sizeof(string) + sizeof(int) + sizeof(int) + sizeof(int);
    
            istream updateFile;
            updateFile.open("prog4.dat");
            if(!updateFile) {
                    cerr << "Open Failure" << endl;
                    exit(1);
            }
    }


Here is the file I will be using for the updates:

1
2
3
4
5
6
7
8
9
10
    10 zidgits 17 -1
    14 lidgits 2 7
    6 gidgits 12 8
    1 bidgits 25 3
    16 widgits 9 10
    7 midgits 0 2
    3 didgits 11 6
    5 tidgits 5 16
    2 pidgits 7 5
    8 kidgits 6 14


Here are some specific questions that I have:

1. How would I go about storing the information from the updateFile to the variables to be written to the output file (hasn't been created yet)?

2. How would I get it to write the data in the correct order since it's based on the last number on each line in the updateFile.

For example, the output file is supposed to start with the lowest stockNumber which is 1, so based on the file, the item with stockNumber 1 is bidgits. The file is then supposed to look at the last number on that line (3) and write the information for the item with that stockNumber (didgits) and so on.

Those are the main questions that jump out at me right now, but I'm sure more will pop up as this progresses. Any help would be greatly appreciated. Also, this is due in about 3 hours, so I'm trying to stick as closely as I can to the code I already have (I know much more will be added of course) if at all possible.
Please don't double post mate.
Sorry, I'm just desperate. I deleted the post from the Beginners forum.
Last edited on
Topic archived. No new replies allowed.