Hi,
I wanted to write a simple data structure to a file for a client application. This is for demo
purpose and so I did not want to write a whole lot of code or spend my time on the storage as
the final version, the BiometricRecord Data will be stored to a database. Anyway I've spent too
much of my time already to give up :-) so if any of you have a suggestion on how to fix the
reading the data back from the file to the archive, it would be a great help ! .


I am able to write my data to the archive, but I am unable to read from it, or I read blank info ?
As the record count is always right while reading of files but it never reads the record and
throws some exception, plus I don't have the debug version of some support files so in release
mode its impossible to track down problems.


Thanks for taking the time


Best Regards
Ruben Gerad Mathew.

Code:
BIOMETRICRECORD.H 
==========================

#pragma once
#include "afx.h"

class BiometricRecord :	public CObject
{
public:
	long m_ID;
	CString m_FirstName;
	CString m_MiddleName;
	CString m_LastName;
	long m_Day;
	long m_Month;
	long m_Year;
	bool m_Gender;
	CString m_Address;
	CString m_Report;
	CString m_BiometricFile;
	CString m_ImageFile;
	

	DECLARE_SERIAL( BiometricRecord );
	BiometricRecord(void);
	~BiometricRecord(void);
	BiometricRecord(int ID, CString FirstName, CString MiddleName, CString LastName, long Day, long Month, long Year, bool Gender, CString Address, CString Report, CString ImageFile, CString BiometricFile);
	void Serialize( CArchive& archive );
};


BIOMETRICORDREC.CPP
==========================

#include "stdafx.h"
#include "biometricrecord.h"
#include <windef.h>
#include <string>
#include <sstream>
#include <fstream>

IMPLEMENT_SERIAL( BiometricRecord, CObject, 0 );
BiometricRecord::BiometricRecord(int ID, CString FirstName, CString MiddleName, CString LastName, long dd, long mm, long yy, bool Gender, CString Address, CString Report, CString ImageFile, CString BiometricFile)
{
	m_ID = ID;
	m_FirstName=FirstName;
	m_MiddleName=MiddleName;
	m_LastName=LastName;
	m_Day = dd;
	m_Month = mm;
	m_Year = yy; 
	m_Gender = Gender;
	m_Address=Address;
	m_Report=Report;
	m_ImageFile=ImageFile;
	m_BiometricFile=BiometricFile;
}


BiometricRecord::BiometricRecord(void)
{
}

BiometricRecord::~BiometricRecord(void)
{
	m_ID = 0;
	m_FirstName.ReleaseBuffer();
	m_MiddleName.ReleaseBuffer();
	m_LastName.ReleaseBuffer();
	m_Day = 0;
	m_Month = 0;
	m_Year = 0; 
	m_Address.ReleaseBuffer();
	m_Report.ReleaseBuffer();
	m_ImageFile.ReleaseBuffer();
	m_BiometricFile.ReleaseBuffer();
}

void BiometricRecord::Serialize(CArchive& archive)
{
	CObject::Serialize(archive);

	if(archive.IsStoring())
		archive << m_ID << m_FirstName << m_MiddleName << m_LastName << m_Day << m_Month << m_Year << m_Gender << m_Address << m_Report << m_ImageFile << m_BiometricFile;
	else
		archive >> m_ID >> m_FirstName >> m_MiddleName >> m_LastName >> m_Day >> m_Month >> m_Year >> m_Gender >> m_Address >> m_Report >> m_ImageFile >> m_BiometricFile;
}


ENROLLMENT.CPP
==========================
#include "stdafx.h"
#include "Enrollment.h"
#include "BiometricRecord.h"
#include <afxtempl.h>
#include <windef.h>
#include <string>
#include <sstream>
#include <fstream>
#include <exception>

CFile bioFile;
CFileException fileException;
int nRecordID;

CArray<BiometricRecord*, BiometricRecord*> BiometricStore;
// CEnrollment dialog

IMPLEMENT_DYNAMIC(CEnrollment, CDialog)

CEnrollment::CEnrollment(CWnd* pParent /*=NULL*/)
	: CDialog(CEnrollment::IDD, pParent)
{
}

CEnrollment::~CEnrollment()
{	
	bioFile.Close();
	for(int i=0; i<BiometricStore.GetCount(); i++)
	{
		delete BiometricStore.GetAt(i);
		BiometricStore.RemoveAt(i);	
	}
}

BOOL CEnrollment::OnInitDialog()
{
	CDialog::OnInitDialog();
	try
	{
	CString dbBiometric;
	BiometricRecord *record;
	nRecordID=1;

	char bufPath[400];
	::GetModuleFileName(AfxGetApp()->m_hInstance, bufPath, sizeof(bufPath)); 

	dbBiometric.SetString(bufPath);		
	dbBiometric.Trim();		
	dbBiometric.Replace("Enroll.exe", "BiometricDB.rdb");  // We open the Biometric.rdb to store the Biometric Record Contents
	if ( !bioFile.Open( dbBiometric, CFile::modeCreate | CFile::modeReadWrite | CFile::modeNoTruncate, &fileException ) )
	{
		TRACE( "Can't Open Biometric Database: %s, Error = %u\n", pszFileName, fileException.m_cause );
	}	
	
	CArchive ar(&bioFile, CArchive::load);		
	//while((record = (BiometricRecord*)ar.ReadObject(RUNTIME_CLASS(BiometricRecord))!=NULL)
	//while(bioFile.GetPosition()!=bioFile.end) //ERR MSG An attempt was made to access an unnamed file past its end ?	
                	bool readFile(true);
		while(readFile)
		{
			CArchive ar(&flBio, CArchive::load);	
			m_pResult.AddString("Reading Record...");
			record = (BiometricRecord*)ar.ReadObject(RUNTIME_CLASS(BiometricRecord));
			if(record==NULL)
				readFile=false;
			
			CString msg;
			BiometricRecord rec(record->m_ID, record->m_FirstName, record->m_MiddleName, record->m_LastName, record->m_Day, record->m_Month, record->m_Year, record->m_Gender, record->m_Address, record->m_Report, record->m_ImageFile, record->m_BiometricFile);
			msg.Format("Reading Record %d [%d, %s %s %s]",RecordID, record->m_ID, record->m_FirstName, record->m_MiddleName, record->m_LastName);
			BiometricDB.Add(&rec);			
			RecordID++;
			m_pResult.AddString(msg);
			ar.Close();
		}
	}catch(...)
	{
		//ERR MSG An attempt was made to access an unnamed file past its end ?  This error could be the possible cause of the failed read attempts.

	}
	//delete record;
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CEnrollment::OnBnClickedOk()
{
	bioFile.SeekToEnd();
	CArchive ar(&bioFile, CArchive::store);
	CString strID, firstName, middleName, lastName, dd, mm, yy, address, report, biometricFile, imageFile;
	GetDlgItem(IDC_ID)->GetWindowText(strID);
	GetDlgItem(IDC_FIRST_NAME)->GetWindowText(firstName);
	GetDlgItem(IDC_MIDDLE_NAME)->GetWindowText(middleName);
	GetDlgItem(IDC_LAST_NAME)->GetWindowText(lastName);
	GetDlgItem(IDC_DD)->GetWindowText(dd);
	GetDlgItem(IDC_MM)->GetWindowText(mm);
	GetDlgItem(IDC_YY)->GetWindowText(yy);
	GetDlgItem(IDC_IMAGE_FILE)->GetWindowText(imageFile);
	GetDlgItem(IDC_BIOMETRIC_FILE)->GetWindowText(biometricFile);
	GetDlgItem(IDC_ADDRESS)->GetWindowText(address);
	GetDlgItem(IDC_REPORT)->GetWindowText(report);

	bool bGen = ((CButton*)GetDlgItem(IDC_MALE))->GetCheck()?true:false;
	BiometricRecord *bioRec = new BiometricRecord(_ttoi(strID), firstName, middleName, lastName, _ttol(dd), _ttol(mm), _ttol(yy), bGen, address, report, imageFile, biometricFile);
	BiometricStore.Add(bioRec);
	ar.WriteObject(bioRec); //Writes contents to file successfully !
	ar.Close();
	nRecordID++;	

	AfxMessageBox("Record Added to Database !");
	DisableEnrollment();	

	//delete bioRec;
	OnOK();
}