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 .
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).
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
thanks.
I will read the textbook.
Oh what a great idea! :icon_idea: Someone going to actually read his textbook :)
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
You can save the entire structure as binary, heres an exmaple of how to:
#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;
}
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
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!
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
Don't worry, it's not so hard. Look at, for example:
http://www.parashift.com/c++-faq-lite/serialization.html
http://www.functionx.com/cpp/articles/serialization.htm
Better think about your class design defects:
1. You have char id_no[5] member then call strcpy(id_no, id) in the constructor. What happens if strlen(id) > 4 ? Right, you overwrite your memory and get a crash or bad data or undefined behaviour (or all three trubles together).
2. You allocate a memory for name by hands (without check up if n_p parameter is 0) then use strcpy to fill it. Why? You include header in the program - use it! Try to declare all text variables in your program as std::string (it's possible and comfort solution in that case).
3. Make obvious "getter" member functions inline (declare them in the class declaration body):
class Aluno
{
protected:
std::string id_no; //char id_no[5];
std::string nome; //char * nome;
static int numero_Alunos;
public:
// add const modifier:
Aluno(const char id[], const char* n_p);
// add for your convinience:
Aluno(const std::string& id, const std::string& nom):
id_no(id), nome(nom)
{}
~Aluno(); // may be trivial in that case.
const char * Get_Id() const { return id_no.c_str(); }
const char * Get_Nome() const
{
return nome.c_str();
}
static int Total_Alunos()
{
return Aluno::numero_Alunas;
}
};
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348