Help Me out with this code

Started by
3 comments, last by JmarsKoder 23 years, 9 months ago
dudes this code is killing me. There is something wrong with it. When I use the write function to write it, then try to read it, it crashes. It is because when it reads in "size" on the size.title is correct. something the others are like rediculous numbers like 153554896. when it should be like 12. If you can fix this please helpe me, here is the code: class entry; //Forward declaration for friend specification struct entrysize //Holds the size of data in an entryclass. { //Used for printing binary data. friend entry; private: long title; long date; long content; long author; long loc; //Location in binary file public: entrysize():title(0),date(0),content(0),author(0),loc(0){} long size() {return title+date+content+author;} long nextspot() {return size()+loc;} //Next spot in file after this }; //entry class entry { private: int id; //The id number, must be set sequentially from 0; string t,d,c,a; entrysize size; public: entry(){} //All self explainitory. entry(const int &i){id=i;} entry(const int &i, const long &l){setup(i,l);} void operator()(const int &i){id=i;} void setup(const int &i, const long &l){id=i; size.loc=l;} void operator()(const int &i, const long &l){setup(i,l);} void set(const string &ti, const string &da, const string &co, const string &au); void operator()(const string &ti, const string &da, const string &co, const string &au) {set(ti,da,co,au);} void read(ifstream &index, ifstream &in, const long &header=0); void write(ofstream &index, ofstream &out, const long &header=0); void read(ifstream &file); void write(ofstream &file); bool read(const string &fn); bool write(const string &fn); bool htmlout(const string &fn); bool textout(const string &fn); void encrypt(const string &key); //Call this intelligently void decrypt(const string &key); long nextspot(){return size.nextspot();} void print(); void printsize(); }; void entry::set(const string &ti, const string &da, const string &co, const string &au) { t=ti; d=da; c=co; a=au; size.title=sizeof(char)*t.size(); size.date=sizeof(char)*d.size(); size.content=sizeof(char)*c.size(); size.author=sizeof(char)*a.size(); } void entry::read(ifstream &index, ifstream &in, const long &header) { //Header is for any info that is placed in the top of the file. index.seekg(header+id*sizeof(entrysize)); index.read(reinterpret_cast(&size),sizeof(entrysize)); in.seekg(size.loc); char *tmp=new char[size.title]; char *tmp2=new char[size.date]; char *tmp3=new char[size.content]; char *tmp4=new char[size.author]; in.read(tmp,size.title); t=tmp; in.read(tmp2,size.date); d=tmp2; in.read(tmp3,size.content); c=tmp3; in.read(tmp4,size.author); a=tmp4; delete[]tmp;delete[]tmp2;delete[]tmp3;delete[]tmp4; } void entry::write(ofstream &index, ofstream &out, const long &header) { index.seekp(header+id*sizeof(entrysize)); index.write(reinterpret_cast(&size),sizeof(entrysize)); out.seekp(size.loc); const char *tmp=t.c_str(); const char *tmp2=d.c_str(); const char *tmp3=c.c_str(); const char *tmp4=a.c_str(); out.write(tmp,size.title); out.write(tmp2,size.date); out.write(tmp3,size.content); out.write(tmp4,size.author); } void entry::print() { cout << t << ''\n'' << d << ''\n'' << a << "\n\n" << c << endl; } void entry::printsize() { cout << size.title << ''\n'' << size.date << ''\n'' << size.content << ''\n'' << size.author << ''\n'' << size.loc << "\n\n"; } void main() { /*ofstream i,f; //Write i.open("i.txt",ios::out); f.open("f.txt",ios::out); entry e[3]; for(int x=0; x<3; x++) { e[x]("I like women","they are cute","i will do them","kit"); if(x==0)e[x](x,0); else{e[x](x,e[x-1].nextspot());} e[x].write(i,f); //e[x].printsize(); } i.close(); f.close(); /**/ /*ifstream i,f; //REad i.open("i.txt",ios::in); f.open("f.txt",ios::in); entry e[3]; for(int x=0; x<3; x++) { if(x==0)e[x](x,0); else{e[x](x,e[x-1].nextspot());} e[x].read(i,f); e[x].printsize(); //e[x].print(); cout << "\n\n"; } i.close(); f.close(); /**/ }
I am XiCI don't do talk, I code: passion is my feul. Use my programs, experience XiC. http://www.x-i-c.com/
Advertisement
That's 136 lines of code. You can expect US to debug it for you. Find out excactly where it's crashing and then post the _only_ the code that's crashing. Then someone can help you.

- Muzzafarath

Mad House Software
The Field Marshals

Edited by - Muzzafarath on June 27, 2000 10:22:08 AM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
well the lines that ar crashing are:

index.write(reinterpret_cast(&size),sizeof(entrysize));

or:

index.read(reinterpret_cast(&size),sizeof(entrysize));

Dude the code looks perfect to me, what can be going wrong?
I am XiCI don't do talk, I code: passion is my feul. Use my programs, experience XiC. http://www.x-i-c.com/
well the lines that ar crashing are:

index.write(reinterpret_cast(&size),sizeof(entrysize));

or:

index.read(reinterpret_cast(&size),sizeof(entrysize));

Dude the code looks perfect to me, what can be going wrong?
I am XiCI don't do talk, I code: passion is my feul. Use my programs, experience XiC. http://www.x-i-c.com/
The part of th code that is crasshing is:

index.write(reinterpret_cast(&size),sizeof(entrysize));

or

index.(reinterpret_cast(&size),sizeof(entrysize));

the code looks perfect to me. What could be wrong?

This topic is closed to new replies.

Advertisement