Originally Posted by
anon
I don't get the ReadSingleRecord function. First you search for a record that has a matching id. If you find it then - instead of printing it right away and returning - you go through some sphagetti to print a whole bunch of records?
The skip is not needed, because you could have done whatever you are trying to skip later, already (if (empty.getaccnum () == id) -> do it here).
If the indentations go too deep, break it up into smaller functions.
(If you say it is very readable, why do you want comments on it? The only function I have seen is not readable at all: starting from the name of the function which seems off.)
u can also read out blocks of 10 records ( times ) with the function, u give in the first record number, it finds the record, and then reads 10 records, because I wanted it to be easy to update, I gave a variable int times too the function so in the future, u can choose howmany records u can read out at once.
ill change the function's name...
Code:
void ReadRecord ( unsigned int id, int times )
{
unsigned int LOCATION = 0; // (location in file)/ sizeof(member)
member empty (" "," "," ", 0 , 0 ); // blank member
std::ifstream ReadOut ("C:\\Datacab.dat", ios::in | ios::binary );
if (!ReadOut)
{
CheckFile ();
}
else
{
ReadOut.seekg ( 0 , ios::beg ); // position file pointer into the begining
while (!ReadOut.eof() ) // while end of file is not reached
{
ReadOut.read ( ( reinterpret_cast < char * > (&empty) ),sizeof (member) );
LOCATION++; // add one too location ( 1 * sizeof (member) )
if (empty.getaccnum () == id )
{
LOCATION--;
break;
}
}
ReadOut.seekg ( ( LOCATION* sizeof (member) ), ios::beg ); // find actual location
for (int x = 0; x < times; x++)
{
if ( empty.getaccnum () == 0 ) // if accnum equals 0, record does not exist
{
std::cout <<"Record Number " << LOCATION << " does not exist" << std::endl;
}
ReadOut.read ( ( reinterpret_cast < char * > (&empty) ), sizeof (member) );
std::cout <<"Record Number " << LOCATION << std::endl;
std::cout <<"_______________\n"
<<"Username: " << empty.getusername () <<"\n"
<<"Email: " << empty.getemail () <<"\n"
<<"Password: " << empty.getpassword () <<"\n"
<<"Age: " << empty.getage () <<"\n"
<<"Accountnumber: " << empty.getaccnum () <<"\n\n"<< std::endl;
}
LOCATION += 1; // location is not the same annymore (LOOP)
}
}