| | |
Save data class
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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).
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.
•
•
Join Date: Aug 2008
Posts: 15
Reputation:
Solved Threads: 2
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>
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
•
•
Join Date: Aug 2008
Posts: 7
Reputation:
Solved Threads: 0
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.
MostrarPessoa = Show Person
If some one help me to solve this problem...I still don't know how to save data.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> #include <string> #include <ctype.h> using namespace std; //DEFINICAO DA SUPERCLASSE PESSOA class Pessoa{ private: char *nome; char *bi; public: Pessoa (char* nome, char *bi); ~Pessoa(); const char* carregaNome() const; const char* carregaBi()const; void lerDadoPessoa( char *nome, char *bi); }; //fim da definicao da classe Pessoa::Pessoa (char * nome_p, char *n_bi) { bi = new char[strlen(n_bi)+1];//criar espaço para o nome nome = new char[strlen(nome_p)+1];//criar espaço nome=nome_p; bi=n_bi; } Pessoa::~Pessoa(){ cout << "\nPessoa " << bi<< "encerrada."; delete [] nome; } inline const char* Pessoa::carregaNome() const{ return nome; } inline const char* Pessoa::carregaBi() const{ return bi; } void MostraPessoa(Pessoa &);//função prototipo Pessoa * criaPessoa(); void MostraPessoa(Pessoa &pess) { cout << "\n\nNome da Pessoa: " << pess.carregaNome(); cout << "\n\nNumero de Bilhete de Identidade: " << pess.carregaBi(); } int main() { cout<< setprecision(2) << setiosflags(ios::fixed) << setiosflags(ios::showpoint); Pessoa* pess_tab[10]; int count=0, i; char resposta; cout <<"\n\nDeseja inserir uma pessoa? (S/N): "; resposta=cin.get(); cin.get(); while (toupper(resposta)=='S' && count < 10 ) { pess_tab[count]=criaPessoa(); ++count; cout <<"\n\nDesja cria mais uma Pessoa? (S/N): "; resposta=cin.get(); cin.get(); } for (int i=0; i<count; ++i) MostraPessoa(*pess_tab[i]); /*for (i=0;i<count;++i) delete pess_tab[i]; */ return 0; } Pessoa * criaPessoa() { char buffer[81]; char id[10]; Pessoa* p_ptr; cout << "\n\n Insira o Nome: "; cin.getline (buffer, 81); cout<<"\n\n Insira o Nu'mero de BI: "; cin.getline (id, 10); cin.get();//limpa o buffer de input de dados p_ptr = new Pessoa(buffer, id); return p_ptr; }
Last edited by Ancient Dragon; Aug 22nd, 2008 at 10:54 pm. Reason: replaced quote tags with code tags
•
•
Join Date: Mar 2008
Posts: 1,495
Reputation:
Solved Threads: 123
You can save the entire structure as binary, heres an exmaple of how to:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; template<typename type> void SaveStruct(ofstream &out, type &obj) { out.write(reinterpret_cast<char*>( &obj ), sizeof type); } template<typename type> void LoadStruct(ifstream &in, type &targetObj) { in.read(reinterpret_cast<char*>( &targetObj ), sizeof type); } struct A { char str[20]; size_t str_len; }; int main() { // Make Structure A a; strcpy_s(a.str, 20, "Hello World"); a.str_len = strlen(a.str); // Save structure to file using SaveStruct(...) ofstream out("savefile.txt", ios::out | ios::binary); SaveStruct(out, a); out.close(); // Load structure from file into the variable b A b; ifstream in("savefile.txt", ios::in | ios::binary); LoadStruct(in, b); // Display struct b cout << "str: " << b.str << "\nstr_len: " << b.str_len; cin.ignore(); return 0; }
•
•
Join Date: Aug 2008
Posts: 7
Reputation:
Solved Threads: 0
I clean the code but i still can't save data.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> #include <string> #include <ctype.h> using namespace std; class Aluno { protected: char id_no[5]; char * nome; static int numero_Alunos; public: Aluno(char id[], char* n_p); ~Aluno(); const char * Get_Id() const; const char * Get_Nome() const; static int Total_Alunos(); }; //Definição Static int Aluno::numero_Alunos = 0; Aluno::Aluno(char id[], char* n_p) { strcpy(id_no, id); //Copia o primeiro argumento para id_no[] nome = new char[strlen(n_p) + 1]; //Cria espaço para o nome strcpy(nome, n_p); //copia o segundo argumento ++numero_Alunos; } Aluno::~Aluno() { cout << "\n\nClass Aluno " << id_no << " encerrada."; delete [] nome; --numero_Alunos; cout << "\nNumero de Alunos inseridos: " << numero_Alunos; } inline const char * Aluno::Get_Id() const { return id_no; } inline const char * Aluno::Get_Nome() const { return nome; } int Aluno::Total_Alunos() { return numero_Alunos; } void Display_Aluno(Aluno &); //Funcão prototipo Aluno * Cria_Aluno(); int main() { cout << setprecision(2) << setiosflags(ios::fixed) << setiosflags(ios::showpoint); Aluno * tab_aluno[10]; int count = 0; char resposta; cout << "\nDeseja criar um Aluno?(S/N): "; resposta = cin.get(); cin.get(); //Limpa o buffer de entrada while (toupper(resposta) == 'S' && count < 10) { tab_aluno[count] = Cria_Aluno(); ++count; cout << "\nDo you want to Cria an Aluno?(S/N): "; resposta = cin.get(); cin.get(); //Limpa o buffer de entrada } //Mostra Alunos for (int i = 0; i < count; ++i) Display_Aluno(*tab_aluno[i]); //Limpa for (int i = 0; i < count; ++i) delete tab_aluno[i]; cout << endl; return 0; } void Display_Aluno(Aluno& novoAluno) { cout << "\n\n\nData for Aluno# " << novoAluno.Get_Id(); cout << "\n\nOwner's Name: " << novoAluno.Get_Nome(); } Aluno * Cria_Aluno() { char id[5]; char buffer[81]; Aluno * ptr_aluno; cout << "\nEnter Aluno ID: "; cin.getline(id, 5); cin.get(); //Limpa o buffer de entrada cout << "\nEnter Aluno Holder's Name: "; cin.getline(buffer, 81); cin.get(); //Limpa o buffer de entrada ptr_aluno = new Aluno (id, buffer); cout << "\n\nNumero de alunos existente: " << Aluno::Total_Alunos() << endl; return ptr_aluno; }
![]() |
Similar Threads
- C++ Classes (C++)
- help me to do operator overloading n class (C++)
- im learning to save vb in acess (VB.NET)
- Python - Importing Data with a Class (Python)
- get posted form data in python (Python)
- not sure how to save fscanf to file (C++)
- Why does my array still empty (C++)
- Class Passing and Returning Arrays (Java)
- How do I create a program using an Array ? (C++)
Other Threads in the C++ Forum
- Previous Thread: Gpa Scores
- Next Thread: Need help debugging code
Views: 2202 | Replies: 11
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






