Save data class

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2008
Posts: 7
Reputation: sonicmas is an unknown quantity at this point 
Solved Threads: 0
sonicmas sonicmas is offline Offline
Newbie Poster

Save data class

 
0
  #1
Aug 20th, 2008
I'm new in c++ programming.

I have to do a program to save data from classes (Student, Teacher and Statistics).

What may i use to save that values? (File, sql...) and how to use.
If some one show me a litle program....
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Save data class

 
0
  #2
Aug 20th, 2008
Just a simple text file will do -- I doubt your instructor wants something as complex as an SQL database.. Use ofstream to output the data and ifstream to read it back. Both are declared in the header file <fstream>.

If you don't yet know how to use those two c++ classes then I'd suggest you read your textbook because it most certainly covers that topic. Also look in the Code Snippets board (link at the top of every DaniWeb page).
Last edited by Ancient Dragon; Aug 20th, 2008 at 6:13 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 7
Reputation: sonicmas is an unknown quantity at this point 
Solved Threads: 0
sonicmas sonicmas is offline Offline
Newbie Poster

Re: Save data class

 
0
  #3
Aug 20th, 2008
thanks.

I will read the textbook.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Save data class

 
0
  #4
Aug 20th, 2008
Originally Posted by sonicmas View Post
thanks.

I will read the textbook.
Oh what a great idea! Someone going to actually read his textbook
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 15
Reputation: findsyntax has a little shameless behaviour in the past 
Solved Threads: 2
findsyntax findsyntax is offline Offline
Newbie Poster

Re: Save data class

 
0
  #5
Aug 21st, 2008
Hi,
I am Rammohan from Bangalore and working as a Technical lead in big IT firm .
Solution for your answer is follows:

Based on my experinace this small problem you can use it normal ASCII file(either plain text file or xml file). Or else if you have a knowledge in database and if your application wants the backend and big style then you go for the SQL.
Depends on your requirement. You can use either one is solve your problem.

Wish You Good luck...
<snip false signature>
Last edited by Ancient Dragon; Aug 21st, 2008 at 7:26 am. Reason: snip false signature
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 7
Reputation: sonicmas is an unknown quantity at this point 
Solved Threads: 0
sonicmas sonicmas is offline Offline
Newbie Poster

Re: Save data class

 
0
  #6
Aug 21st, 2008
This is a litle bit of my program and i have some problems. Pessoa = Person BI=Identification Number
MostrarPessoa = Show Person
If some one help me to solve this problem...I still don't know how to save data.

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <ctype.h>
  5.  
  6. using namespace std;
  7.  
  8. //DEFINICAO DA SUPERCLASSE PESSOA
  9. class Pessoa{
  10. private:
  11. char *nome;
  12. char *bi;
  13. public:
  14. Pessoa (char* nome, char *bi);
  15. ~Pessoa();
  16. const char* carregaNome() const;
  17. const char* carregaBi()const;
  18. void lerDadoPessoa( char *nome, char *bi);
  19.  
  20. };
  21. //fim da definicao da classe
  22.  
  23. Pessoa::Pessoa (char * nome_p, char *n_bi)
  24. {
  25. bi = new char[strlen(n_bi)+1];//criar espaço para o nome
  26. nome = new char[strlen(nome_p)+1];//criar espaço
  27. nome=nome_p;
  28. bi=n_bi;
  29.  
  30. }
  31. Pessoa::~Pessoa(){
  32. cout << "\nPessoa " << bi<< "encerrada.";
  33. delete [] nome;
  34. }
  35.  
  36. inline const char* Pessoa::carregaNome() const{
  37. return nome;
  38. }
  39.  
  40. inline const char* Pessoa::carregaBi() const{
  41. return bi;
  42. }
  43.  
  44. void MostraPessoa(Pessoa &);//função prototipo
  45. Pessoa * criaPessoa();
  46.  
  47. void MostraPessoa(Pessoa &pess)
  48. {
  49. cout << "\n\nNome da Pessoa: " << pess.carregaNome();
  50. cout << "\n\nNumero de Bilhete de Identidade: " << pess.carregaBi();
  51. }
  52.  
  53. int main()
  54. {
  55. cout<< setprecision(2)
  56. << setiosflags(ios::fixed)
  57. << setiosflags(ios::showpoint);
  58. Pessoa* pess_tab[10];
  59.  
  60. int count=0, i;
  61. char resposta;
  62.  
  63. cout <<"\n\nDeseja inserir uma pessoa? (S/N): ";
  64. resposta=cin.get();
  65. cin.get();
  66.  
  67. while (toupper(resposta)=='S' && count < 10 )
  68. {
  69. pess_tab[count]=criaPessoa();
  70. ++count;
  71. cout <<"\n\nDesja cria mais uma Pessoa? (S/N): ";
  72. resposta=cin.get();
  73. cin.get();
  74. }
  75.  
  76. for (int i=0; i<count; ++i)
  77. MostraPessoa(*pess_tab[i]);
  78.  
  79. /*for (i=0;i<count;++i)
  80. delete pess_tab[i];
  81. */
  82. return 0;
  83. }
  84.  
  85. Pessoa * criaPessoa()
  86. {
  87. char buffer[81];
  88. char id[10];
  89. Pessoa* p_ptr;
  90.  
  91. cout << "\n\n Insira o Nome: ";
  92. cin.getline (buffer, 81);
  93.  
  94. cout<<"\n\n Insira o Nu'mero de BI: ";
  95. cin.getline (id, 10);
  96.  
  97. cin.get();//limpa o buffer de input de dados
  98.  
  99. p_ptr = new Pessoa(buffer, id);
  100.  
  101. return p_ptr;
  102. }
Last edited by Ancient Dragon; Aug 22nd, 2008 at 10:54 pm. Reason: replaced quote tags with code tags
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,495
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 123
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Save data class

 
0
  #7
Aug 22nd, 2008
You can save the entire structure as binary, heres an exmaple of how to:
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. template<typename type>
  6. void SaveStruct(ofstream &out, type &obj) {
  7. out.write(reinterpret_cast<char*>( &obj ), sizeof type);
  8. }
  9.  
  10. template<typename type>
  11. void LoadStruct(ifstream &in, type &targetObj) {
  12. in.read(reinterpret_cast<char*>( &targetObj ), sizeof type);
  13. }
  14.  
  15. struct A {
  16. char str[20];
  17. size_t str_len;
  18. };
  19.  
  20. int main() {
  21.  
  22. // Make Structure
  23. A a;
  24. strcpy_s(a.str, 20, "Hello World");
  25. a.str_len = strlen(a.str);
  26.  
  27. // Save structure to file using SaveStruct(...)
  28. ofstream out("savefile.txt", ios::out | ios::binary);
  29. SaveStruct(out, a);
  30. out.close();
  31.  
  32. // Load structure from file into the variable b
  33. A b;
  34. ifstream in("savefile.txt", ios::in | ios::binary);
  35. LoadStruct(in, b);
  36.  
  37. // Display struct b
  38. cout << "str: " << b.str << "\nstr_len: " << b.str_len;
  39.  
  40. cin.ignore();
  41. return 0;
  42. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Save data class

 
0
  #8
Aug 22nd, 2008
Regrettably, sonicmas can't follow williamhemsworth's advice without serious troubles: it's a big mistake to save Pessoa class objects as binaries with proposed SaveStruct template!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 7
Reputation: sonicmas is an unknown quantity at this point 
Solved Threads: 0
sonicmas sonicmas is offline Offline
Newbie Poster

Re: Save data class

 
0
  #9
Aug 22nd, 2008
What can i do to save it? I can't find nothing that help me saving that class files. Exist a lot of information explain how to save data using structs but nothing about class. It's something whith me?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 7
Reputation: sonicmas is an unknown quantity at this point 
Solved Threads: 0
sonicmas sonicmas is offline Offline
Newbie Poster

Re: Save data class

 
0
  #10
Aug 23rd, 2008
I clean the code but i still can't save data.

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <ctype.h>
  5.  
  6. using namespace std;
  7.  
  8. class Aluno
  9. {
  10. protected:
  11. char id_no[5];
  12. char * nome;
  13. static int numero_Alunos;
  14.  
  15. public:
  16. Aluno(char id[], char* n_p);
  17. ~Aluno();
  18. const char * Get_Id() const;
  19. const char * Get_Nome() const;
  20. static int Total_Alunos();
  21. };
  22.  
  23.  
  24. //Definição Static
  25. int Aluno::numero_Alunos = 0;
  26.  
  27.  
  28. Aluno::Aluno(char id[], char* n_p)
  29. {
  30. strcpy(id_no, id); //Copia o primeiro argumento para id_no[]
  31.  
  32. nome = new char[strlen(n_p) + 1]; //Cria espaço para o nome
  33.  
  34. strcpy(nome, n_p); //copia o segundo argumento
  35.  
  36. ++numero_Alunos;
  37. }
  38.  
  39. Aluno::~Aluno()
  40. {
  41. cout << "\n\nClass Aluno " << id_no << " encerrada.";
  42. delete [] nome;
  43.  
  44. --numero_Alunos;
  45.  
  46. cout << "\nNumero de Alunos inseridos: " << numero_Alunos;
  47. }
  48.  
  49. inline const char * Aluno::Get_Id() const
  50. {
  51. return id_no;
  52. }
  53.  
  54. inline const char * Aluno::Get_Nome() const
  55. {
  56. return nome;
  57. }
  58.  
  59.  
  60. int Aluno::Total_Alunos()
  61. {
  62. return numero_Alunos;
  63. }
  64.  
  65.  
  66. void Display_Aluno(Aluno &); //Funcão prototipo
  67. Aluno * Cria_Aluno();
  68.  
  69. int main()
  70. {
  71. cout << setprecision(2)
  72. << setiosflags(ios::fixed)
  73. << setiosflags(ios::showpoint);
  74.  
  75. Aluno * tab_aluno[10];
  76.  
  77. int count = 0;
  78. char resposta;
  79.  
  80. cout << "\nDeseja criar um Aluno?(S/N): ";
  81. resposta = cin.get();
  82. cin.get(); //Limpa o buffer de entrada
  83.  
  84. while (toupper(resposta) == 'S' && count < 10)
  85. {
  86. tab_aluno[count] = Cria_Aluno();
  87.  
  88. ++count;
  89.  
  90. cout << "\nDo you want to Cria an Aluno?(S/N): ";
  91. resposta = cin.get();
  92. cin.get(); //Limpa o buffer de entrada
  93. }
  94.  
  95. //Mostra Alunos
  96.  
  97. for (int i = 0; i < count; ++i)
  98. Display_Aluno(*tab_aluno[i]);
  99.  
  100. //Limpa
  101.  
  102. for (int i = 0; i < count; ++i)
  103. delete tab_aluno[i];
  104.  
  105. cout << endl;
  106. return 0;
  107. }
  108.  
  109. void Display_Aluno(Aluno& novoAluno)
  110. {
  111. cout << "\n\n\nData for Aluno# " << novoAluno.Get_Id();
  112.  
  113. cout << "\n\nOwner's Name: " << novoAluno.Get_Nome();
  114.  
  115. }
  116.  
  117. Aluno * Cria_Aluno()
  118. {
  119. char id[5];
  120. char buffer[81];
  121.  
  122. Aluno * ptr_aluno;
  123.  
  124. cout << "\nEnter Aluno ID: ";
  125. cin.getline(id, 5);
  126.  
  127. cin.get(); //Limpa o buffer de entrada
  128.  
  129. cout << "\nEnter Aluno Holder's Name: ";
  130. cin.getline(buffer, 81);
  131.  
  132. cin.get(); //Limpa o buffer de entrada
  133.  
  134. ptr_aluno = new Aluno (id, buffer);
  135.  
  136. cout << "\n\nNumero de alunos existente: "
  137. << Aluno::Total_Alunos() << endl;
  138.  
  139. return ptr_aluno;
  140. }
Reply With Quote Quick reply to this message  
Reply

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




Views: 2202 | Replies: 11
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC