what in the heck randomaccess

so I'm still messing around with random access of a text file anyway I am trying to figure out the math in able to store the data correctly in a text file so I can load my person objects dynamically with out it them being stored in ram,

anyway I got some crazy output when I added + 4 to the end but when i type + 5 or any number greater or less than 4 I don't get the cray results

here is the results of the text file when I have 4 at the end
㨲ㄲ〺㈺㨱㨴㨱㔲㔺:㄀㔺㈺ㄺ㨰

and here is the results when I have 5 at the end

2:22:0:22:4:1:26:5: 1:5:2:10:

why the heck is some letters of the chinesse alphabet getting printed out ? lol



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
  #include <iostream>
#include <fstream>
#include <vector>
#include <map>

using namespace std;


class person{

  public:
      int data1;
      int data2;
      person(int d1,int d2){
        data1 = d1;
        data2 = d2;
      }
};

class index{

public:
    int locationID;
    int startByte;
    int size;

    index(){


    }

    index(int sb,int s){

       startByte = sb;
       size = s;
    }

     index(const index& other){

       startByte = other.startByte;
       locationID = other.locationID;
       size = other.size;
    }

    index& operator=(const index& other){

       startByte = other.startByte;
       locationID = other.locationID;
       size = other.size;

       return *this;
    }
};

int main()
{

    ofstream out;
    ifstream in;
    vector<person> people;
    map<int,index> indexs;

    person one(1,5);
    person two(2,10);

    people.push_back(one);
    people.push_back(two);

    out.open("ra2.txt");

    int size = people.size();
    out << size;
    out << ":";
    int indexSize = (int)(1 * 3 * size);
    int personStart = (int)(out.tellp() + indexSize + 1 + 2 + 3 * size + 4);
    out << personStart;
    out << ":";
    int indexStart = (int)out.tellp();
    int startPointer = personStart;

    out.seekp(personStart);

    for(int i = 0; i < people.size(); i++){

        out << people[i].data1;
        out << ":";
        out << people[i].data2;
        out << ":";


        index ind(startPointer,(int)(out.tellp() - startPointer));
        ind.locationID = i;
        indexs[i] = ind;

        startPointer = (int) (out.tellp());
    }

    out.seekp(indexStart);

    for(map<int,index>::iterator it = indexs.begin();it != indexs.end();it++){

        out << it->second.locationID;
        out << ":";
        out << it->second.startByte;
        out << ":";
        out << it->second.size;
        out << ":";
    }

   out.close();


}
is the data in the text files fixed width such that each record is N bytes and you can skip from record to record via a multiple of N and internally skip fields by using the widths of each field in order totaled up?

random access files need to be fixed width data fields.
Ho Jonnin it seems to work I tested the outputs and everything seems correct but for some reason and I can't figure out for the life of me why,the size of the first index is 4 and size of the second index is 5?

yet it still works great when skipping to that particular byte

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

 ifstream in;
    stringstream ss;
    map<int,index> inds;

    in.open("ra2.txt");

    int size;
    string sizeString;
    getline(in,sizeString,':');
    ss << sizeString;
    ss >> size;
    ss.clear();
    ss.str(string());
    int personStart;
    string psString;
    getline(in,psString,':');
    ss << psString;
    ss >> personStart;
    ss.clear();
    ss.str(string());

    while(in.tellg() < personStart){

        int locationId;
        string idString;

        getline(in,idString,':');
        ss << idString;
        ss >> locationId;
        ss.clear();
        ss.str(string());

        int startByte;
        string sbString;
        getline(in,sbString,':');
        ss << sbString;
        ss >> startByte;
        ss.clear();
        ss.str(string());

        int indexSize;
        string sizeString;
        getline(in,sizeString,':');
        ss << sizeString;
        ss >> indexSize;
        ss.clear();
        ss.str(string());

        index temp(startByte,indexSize);
        temp.locationID = locationId;

        inds[locationId] = temp;
    }

    for(map<int,index>::iterator it = inds.begin(); it != inds.end(); it++){


        in.seekg(it->second.startByte);
        int data1,data2;
        string d1String,d2String;

        ss.clear();
        ss.str(string());

        getline(in,d1String,':');
        ss << d1String;
        ss >> data1;
        ss.clear();
        ss.str(string());

        getline(in,d2String,':');
        ss << d2String;
        ss >> data2;
        ss.clear();
        ss.str(string());

        cout << "data 1: " << data1 << " data 2: " << data2 << endl;

    }

    int choice;
    cout << "enter a number you want 1 or 2 " << endl;
    cin >> choice;
    if(choice < 1 || choice > inds.size()){
        return 1;
    }else{

       in.seekg(inds[choice-1].startByte); // -1 because indexs start at 0
       int d1,d2;
       string s1,s2;

       getline(in,s1,':');
       ss << s1;
       ss >> d1;
       ss.clear();
       ss.str(string());

       getline(in,s2,':');
       ss << s2;
       ss >> d2;
       ss.clear();
       ss.str(string());

       cout << "data 1 : " << d1 << "data 2 : " << d2 << endl;
    }
I can't figure out for the life of me why,the size of the first index is 4 and size of the second index is 5?

As jonnin pointed out the records need to be the same size, but they are not.
1
2
  person one(1,5);
  person two(2,10);

In text mode 10 obviously need 2 chars and 5 only one.
Normally you calculate the offset of a record by (rec number * sizeof(rec)) and set the file pointer to this position and read the record in one go. For random access normally a binary mode is used.
Topic archived. No new replies allowed.