Jump to content
  • Advertisement

Archived

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

JmarsKoder

Help Me out with this code

This topic is 6543 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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(); /**/ }

Share this post


Link to post
Share on other sites
Advertisement
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!