Writing Derived Objects to files

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2008
Posts: 20
Reputation: TeCNoYoTTa is an unknown quantity at this point 
Solved Threads: 0
TeCNoYoTTa's Avatar
TeCNoYoTTa TeCNoYoTTa is offline Offline
Newbie Poster

Writing Derived Objects to files

 
0
  #1
Jul 4th, 2008
hello all
i want to know the proplem with this program

and first ........ can i write a string object to a file normally like other objects ??

this is the code ......the program writes the file but it can't read it again

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. enum ClassType
  8. {
  9. STUDENT, PROF
  10. };
  11.  
  12. class person
  13. {
  14. protected:
  15. int id;
  16. string name;
  17. static vector<person*> arr;
  18. public:
  19.  
  20. person() : id(0), name("")
  21. {
  22. }
  23.  
  24. virtual void GetData()
  25. {
  26. cout << "ID:";
  27. cin >> id;
  28. cout << "Name:";
  29. cin >> name;
  30. }
  31.  
  32. virtual void ShowData()
  33. {
  34. cout << "ID:" << id << "\nName:" << name;
  35. }
  36.  
  37. virtual ClassType GetType();
  38. static void Add();
  39. static void ShowAll();
  40. static void Save();
  41. static void Load();
  42. };
  43. vector<person*> person::arr;
  44.  
  45. class prof : public person
  46. {
  47. protected:
  48. int students;
  49. int years;
  50.  
  51. public:
  52.  
  53. prof()
  54. {
  55. person();
  56. students = 0;
  57. years = 0;
  58. }
  59.  
  60. void GetData()
  61. {
  62. person::GetData();
  63. cout << "Students No.:";
  64. cin >> students;
  65. cout << "Years No.:";
  66. cin >> years;
  67. }
  68.  
  69. void ShowData()
  70. {
  71. person::ShowData();
  72. cout << "\nStudents No.:" << students << "\nYears No.:" << years << endl;
  73. }
  74.  
  75. };
  76.  
  77. class student : public person
  78. {
  79. protected:
  80. int AVGGrade;
  81. int year;
  82. public:
  83.  
  84. student()
  85. {
  86. person();
  87. AVGGrade = 1;
  88. year = 1;
  89. }
  90.  
  91. void GetData()
  92. {
  93. person::GetData();
  94. cout << "Student Grade.:";
  95. cin >> AVGGrade;
  96. cout << "Year No.:";
  97. cin >> year;
  98. }
  99.  
  100. void ShowData()
  101. {
  102. person::ShowData();
  103. cout << "\nStudent AVGG.:" << AVGGrade << "\nYear No.:" << year << endl;
  104. }
  105. };
  106.  
  107. ClassType person::GetType()
  108. {
  109. if (typeid (*this) == typeid (prof))
  110. return PROF;
  111. else if (typeid (*this) == typeid (student))
  112. return STUDENT;
  113. }
  114.  
  115. void person::Add()
  116. {
  117. char c;
  118. cout << "p for prof and s for student:";
  119. cin >> c;
  120. person *ptr;
  121. switch (c)
  122. {
  123. case 'p':
  124. ptr = new prof;
  125. arr.push_back(ptr);
  126. break;
  127. case 's':
  128. ptr = new student;
  129. arr.push_back(ptr);
  130. }
  131. (arr.back())->GetData();
  132. }
  133.  
  134. void person::ShowAll()
  135. {
  136. for (int i = 0; i < arr.size(); i++)
  137. arr[i]->ShowData();
  138. }
  139.  
  140. void person::Save()
  141. {
  142. ofstream file("DATA.bin", ios::binary | ios::trunc);
  143. ClassType CurrentObj;
  144. int size;
  145. for (int i = 0; i < arr.size(); i++)
  146. {
  147. CurrentObj = arr[i]->GetType();
  148. //cout << "\nH1\n";
  149. file.write(reinterpret_cast<char*> (&CurrentObj), sizeof (ClassType));
  150. //cout << "\nH2\n";
  151. switch (CurrentObj)
  152. {
  153. case PROF:
  154. size = sizeof (prof);
  155. break;
  156. case STUDENT:
  157. size = sizeof (student);
  158. }
  159. file.write(reinterpret_cast<char*> (arr[i]), size);
  160. switch (CurrentObj)
  161. {
  162. case PROF:
  163. cout << "\nI Wrote " << size << " Of Type Prof";
  164. break;
  165. case STUDENT:
  166. cout << "\nI Wrote " << size << " Of Type Student";
  167. }
  168.  
  169. }
  170. }
  171.  
  172. void person::Load()
  173. {
  174. ifstream file("DATA.bin", ios::binary);
  175. ClassType CurrentObj;
  176. person *TempObj;
  177. while (file)
  178. {
  179. file.read(reinterpret_cast<char*> (&CurrentObj), sizeof (ClassType));
  180. if (CurrentObj == PROF)
  181. {
  182. TempObj = new prof;
  183. cout << "\nReading Prof";
  184. file.read(reinterpret_cast<char*> (TempObj), sizeof (prof));
  185. cout << "\nProf Was Read";
  186. }
  187. else if (CurrentObj == STUDENT)
  188. {
  189. TempObj = new student;
  190. cout << "\nReading Student";
  191. file.read(reinterpret_cast<char*> (TempObj), sizeof (student));
  192. cout << "\nStudent Was Read\n\n";
  193. }
  194.  
  195. TempObj->ShowData();
  196. }
  197. system("pause");
  198. }
  199.  
  200. int main(int argc, char* argv[])
  201. {
  202. char c;
  203. cin >> c;
  204. switch (c)
  205. {
  206. case 'a':
  207. person::Add();
  208. person::Add();
  209. person::Save();
  210. break;
  211. case 'l':
  212. person::Load();
  213. }
  214. //person::Add();
  215. //person::Add();
  216. //person::Add();
  217. //person::Save();
  218. //perosn::Load();
  219. //person::ShowAll();
  220. return 0;
  221. }
Last edited by TeCNoYoTTa; Jul 4th, 2008 at 5:08 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,652
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 723
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Writing Derived Objects to files

 
1
  #2
Jul 4th, 2008
Using read and write to serialize your objects is generally a bad idea. At the risk of being too vague, I'll distill all of my experience into one piece of advice for your convenience:

When you want to serialize an object manually, convert each of the fields to a string, format them as desired, and then write the string to a file. When you want to deserialize an object manually, read the string from your file, parse the string in a constructor, and populate the data members.

In code form, such a solution would look like this:
  1. #include <sstream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. class student {
  6. public:
  7. std::string name;
  8. std::vector<std::string> classes;
  9. std::vector<int> grades;
  10. public:
  11. student() {}
  12. student ( const std::string& serialized )
  13. {
  14. std::stringstream src ( serialized );
  15. std::string class_name;
  16. int grade;
  17.  
  18. // No error checking
  19. std::getline ( src, name, '|' );
  20.  
  21. while ( std::getline ( src, class_name, '|' ) ) {
  22. classes.push_back( class_name );
  23. src>> grade;
  24. grades.push_back ( grade );
  25. }
  26. }
  27. public:
  28. std::string serialize ( std::ostream& out )
  29. {
  30. // Assume grades and classes are parallel
  31. // No error checking
  32. std::stringstream serialized;
  33.  
  34. serialized<< name <<'|';
  35.  
  36. std::vector<std::string>::size_type i;
  37.  
  38. for ( i = 0; i < classes.size(); i++ ) {
  39. if ( i != 0 )
  40. serialized<<'|';
  41.  
  42. serialized<< classes[i] <<'|'<< grades[i];
  43. }
  44.  
  45. out<< serialized.str();
  46.  
  47. return serialized.str();
  48. }
  49. };
This way you don't have to know the POD type rules, you don't have to worry about what's undefined behavior and what isn't, and you retain maximum portability.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 20
Reputation: TeCNoYoTTa is an unknown quantity at this point 
Solved Threads: 0
TeCNoYoTTa's Avatar
TeCNoYoTTa TeCNoYoTTa is offline Offline
Newbie Poster

Re: Writing Derived Objects to files

 
0
  #3
Jul 4th, 2008
thanks Very much
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC