random access ofstream
Dec 14, 2017 at 5:15pm UTC
Hi guys I am trying to write to a file so I can access it randomly,I will use indexes to get where each persons data members are stored in a txt file
however this is not working the way I expected it to,when I open up the file there is data written to it,but the data is all numbers with no strings in it even though I did add strings
I'm guessing maybe my math is off? or my logic is off I thought it would work or else I am misusing seekp and tellp?
thanks
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
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
using namespace std;
class Index{
public :
int locationID;
int startByte;
int size;
Index(){ }
Index(int s,int siz){
startByte = s;
size = siz;
}
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 ;
}
};
class Person{
public :
string name;
int age;
Person(){}
Person(string n,int a){
name = n;
age = a;
}
Person(const Person& other){
name = other.name;
age = other.age;
}
Person& operator =(const Person& other){
name = other.name;
age = other.age;
return *this ;
}
};
int main()
{
ofstream out;
out.open("randomAccess.txt" );
vector<Person> people;
map<int ,Index> index;
for (int i = 0; i < 50; i++){
Person temp("adam" ,i);
people.push_back(temp);
}
try {
int size = people.size();
out << size;
int indexSize(people.size() * 3 * sizeof (int ));
int personStart = (int )(out.tellp() + indexSize + sizeof (int ));
out << personStart;
int indexStart = (int )out.tellp();
out << indexStart;
int startPointer = personStart;
out.seekp(personStart);
for (int i = 0; i < people.size(); i++){
out << people[i].name;
out << people[i].age;
startPointer = (int ) out.tellp();
Index ind(startPointer,(int )(out.tellp() - startPointer));
index[i] = ind;
startPointer = (int ) (out.tellp());
}
out.seekp(indexStart);
for (map<int ,Index>::iterator it = index.begin(); it != index.end(); it++ ){
int number = it->first;
out << number;
out << it->second.startByte;
out << it->second.size;
}
}catch (int e){
cout << e << endl;
}
}
Dec 14, 2017 at 7:10pm UTC
after a few couts I have come to the conclusion the my math or calculations are probably off,
but does anybody know why my calculations are off?
Dec 14, 2017 at 8:45pm UTC
I did the math and I ended up getting the size of the index by int indexSize = (10 * 1) + (40 * 2) + 50 + (3 * 50) + 50 +(3 * 50) + 50 + (50*1) + 50
but my calculations must be wrong,I'm not sure why though as each character is a bye so I thought I took every byte into consideration
anyway when I subtracted 199 I seemed to get the correct position,but how what was I missing?
int indexSize = (10 * 1) + (40 * 2) + 50 + (3 * 50) + 50 +(3 * 50) + 50 + (50*1) + 50-199;
Dec 15, 2017 at 5:36pm UTC
I’ve no problem to confess I got lost after a few lines of your try-block, so I didn’t even try to do the math. But I wonder why you are so interested in the sizeof(int), since you open your file as text, not binary, therefore you should consider what you write are characters.
I hope the following code could help to clarify my question:
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
#include <fstream>
#include <iostream>
#include <limits>
void waitForEnter();
int main()
{
std::ofstream out("ra.txt" );
if (!out) {
std::cout << "can't open output file.\n" ;
return 0;
}
std::streampos pos1 = out.tellp();
std::cout << "At beginning, tellp() says " << pos1 << '\n' ;
out << 'a' ;
std::streampos pos2 = out.tellp();
std::cout << "After writing 'a' tellp() says " << pos2
<< " which is " << pos2 - pos1 << " distant\n" ;
out << 9;
pos1 = out.tellp();
std::cout << "After writing 9 tellp() says " << pos1
<< " which is " << pos1 - pos2 << " distant\n" ;
out << 13;
pos2 = out.tellp();
std::cout << "After writing 13 tellp() says " << pos2
<< " which is " << pos2 - pos1 << " distant\n" ;
out << 666.13;
pos1 = out.tellp();
std::cout << "After writing 666.13 tellp() says " << pos1
<< " which is " << pos1 - pos2 << " distant\n" ;
out << "Hi, adam2016" ; // 12 characters
pos2 = out.tellp();
std::cout << "After writing \"Hi, adam2016\" tellp() says " << pos2
<< " which is " << pos2 - pos1 << " distant\n" ;
waitForEnter();
}
void waitForEnter()
{
std::cout << "\nPress ENTER to continue...\n" ;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );
}
Output
At beginning, tellp() says 0
After writing 'a' tellp() says 1 which is 1 distant
After writing 9 tellp() says 2 which is 1 distant
After writing 13 tellp() says 4 which is 2 distant
After writing 666.13 tellp() says 10 which is 6 distant
After writing "Hi, adam2016" tellp() says 22 which is 12 distant
Press ENTER to continue...
ra.txt:
a913666.13Hi, adam2016
Dec 16, 2017 at 1:24pm UTC
hey Enoziat
very good point I noticed this too after a while lol,I forgot that I was working with text rather than binary so each character is one byte long
thanks
Topic archived. No new replies allowed.