This topic is now archived and is closed to further replies.


c++, need help

Recommended Posts

Cprog    122
This might seem like I have no idea what i''m doing, maybe I don''t but I could use some help. The goal of the program is to manaages a huge one-dimension array of doubles as a disk file, but which can be used in a program as if it is kept in memory. This program is to used for placing a double value into any array element from the disk file and vice versa put any element value into a double variable. Only binary no ASCII. My problem is the subscript operator and the << operator, I''m have no idea on how to make it work, any advice on how to work this is deeply thanks. #include #include #include class Darray { private: long amountofindex; fstream *myfile; double fake; double counter; public: int place; Darray(char *, long); int & operator[](int i); friend void operator << (Darray &, double &); friend void operator << (double &, Darray &); //~Darray(); }; Darray::Darray(char * filename, long size){ fstream myfile(filename, ios::in|ios::out|ios::binary); if(myfile.good()) { for (int i = 0; i < size; i++){ myfile.write((char *)&fake, sizeof(fake)); counter++; } myfile.seekg(0,ios::end); amountofindex = myfile.tellg()/sizeof(double); myfile.seekg(0, ios::beg); if(myfile.eof()) myfile.clear(); // clear eof flag else { cerr << "error in reading " << filename << ".\n"; } } } int & Darray::operator[](int i) { if(i < 0 || i >= counter) { cout << "Invalid record number\n"; } else streampos place = i * sizeof (double) ; return place; } void operator << (Darray &d, double &n) { d[place] << n; } void operator << (double &n, Darray &d) { n << d[place]; } int main() { int i; double d99; Darray hugedoub("doublefile.bin", 10); // uses ( or initializes) a disk array of 100000 doubles d99 = 1234.5678; hugedoub[0] << 100.111; // sets element 0 to value hugedoub[1] << d99; cout << hugedoub[0] << endl; cout << hugedoub[1]; return 0; }

Share this post

Link to post
Share on other sites