hello guys, i know m rather new, so ill also give a quick introduction:
my name is Sven Vranken, i live in belgium and im aged 16. in my free time i like to go out with frends, work out at the jym and i also like to go skateboarding some times.
i first started programming cpp one year ago, im learning it at home.
well m not so good in intro's so thats it for me.
to be honest i came to this forum cause im totaly desperate. i cant seem to figure out my fault in my program. ill first tell you whats it all about.
im making a program (almost complete) that makes a random acces data file, and i write 2 classes to it which share inheritance. after you write specific information to the file you can read it out later. but theirs something wrong, i searched alot and i figured out the problem is hiding in the part where the actual data file is build, the code is a bit to big to paste here (about 250 lines) so im going to paste the part where i think the problem is and m going to include the whole project in a .rar file for those who want to look further in it.
thanx to annyone who replies
Code:
switch (choise)
{
case 1: //chosen to create the file
{
cout <<" Create or renew the data file\n"<< endl;
ofstream basic ("clients.dat",ios::binary); // create a stream to output clients to //file ( im going to write 50 empty clients of each class to the file )
if (!basic)
{
cout <<"File cannot be opened"<<endl;
exit(1);
}
baseclass baseclient; // create a basic client default constructor fills in data which
// is going to be written to the file
for (int x = 1; x<= 51; x++)
{
baseclient.setID(x);
basic.write (reinterpret_cast < const char * > (&baseclient),
sizeof (baseclass)); // write it binary
}
basic.close();
/*
* im going to close the stream and create a new one to write the second class to it
* altough this is not necissary, its better to understand;
*/
cout <<"50 empty basic accounts created"<<endl;
ofstream advanced ("clients.dat", ios::binary | ios::app);
dirclass advancedclient; // create advancedclient (inherits from baseclient)
for (int x = 52; x <= 102; x++)
{
advancedclient.setID(x);
advanced.write(reinterpret_cast < const char* > (&advancedclient),
sizeof (dirclass));
cout << x << endl;
}
cout <<" 50empty advanced accounts created" << endl;
cout <<"file (re)created with succes!"<<endl;
and when i read the file out this is some of the output:
Code:
50
3 0
//baseclient id 50, 51 and so on (output right)
// the spaces also are a part of the output
51
3 0
-----------READ FIRST 50 BASIC ACCOUNTS------------ // this worked out well.
52
3 0 0
/* FIRST ADVANCED CLIENT
outputs right!(all the id numbers (53,54.. also print out right but all the actual data behind that dont print out right */
53 `ø ø
0 0 0
‘|À$
54 `ø ø
0 0 0
‘|À$
if u want to look in the whole source:
(i could not upload all the files so here is the smallest part of it (the functions that are used in main ( i seperated them cause its easyer to read)
:
Mainfunctions.cpp :
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include "header2.h"
#include <sstream>
using namespace std;
void printmainoptions ()
{
cout <<"1. To Create or renew the data file" << endl;
cout <<"2. To add or modify a new user" << endl;
cout <<"3. To view all the users" << endl;
cout <<"4. To exit" << endl;
cout <<"\n"<<endl;
}
void writeinfo ()
{
cout <<"You can add a Basic user or an Advanced user\n"
<<"To add a Basic user: "
<<"please input an accountnumber from 1 to 51\n"
<<"To add an Advanced user: "
<<"please input an accountnumber from 51 to 101\n"
<< endl;
}
int getchoise ()
{
int choise;
string kak;
getline (cin,kak);
stringstream(kak) >> choise;
while (choise < 1 || choise > 103)
{
cout <<"Please input a valid number: ";
getline (cin,kak);
stringstream(kak) >> choise;
}
return (choise);
}
void outputline (ostream &output,baseclass &lol )
{
output <<left<<setw(5)<<lol.getID()
<<setw(15)<<lol.getfname()
<<setw(15)<<lol.getlname()
<<"\n"<<setw(7)<<lol.getsex()
<<setw(2)<<lol.getage()<<"\n"
<<setw(40)<<lol.getwoonplaats()<<"\n"<<endl;
}
void Outputline (ostream &output,dirclass &loll)
{
output <<left<<setw(5)<<loll.getID()
<<setw(15)<<loll.getfname()
<<setw(15)<<loll.getlname()
<<"\n"<<setw(7)<<loll.getsex()
<<left<<setw(4)<<loll.getage()
<<setw(8)<<loll.getsalary()<<"\n"
<<left<<setw(40)<<loll.getwoonplaats()<<"\n"
<<left<<setw(100)<<loll.raboutyourself()<<endl;
}
i realy hope u guys can help me