fFile01Dlg.cpp

 // fFile01Dlg.cpp : implementation file
// This program was written by Michael Duong
// to help programmers learn how easy it is
// to create a effective database program.


//*IMPORTANT* This program will create a database file in your c drive
//by the name of database.dat.  If at any time you want to edit that file
//you may open it with notepad.  IF at any time you want to change
//the location of where this program writes its database file, please scroll
//below and I have pointed out where you can change it.


//If you want to create a program similar like this one all over again,
//you may go to File>New, then click on the projects tab, then choose
//MFC AppWizard(exe) from the list. Enter a project name, then press Ok,
//then press finish.

//You will also need to add member variables, create your form etc.  This
//will take too long to explain.  But if you need to know, please email me
//at bebeboi916@aol.com
#include "stdafx.h"
#include "fFile01.h"
#include "fFile01Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CFile MyFile;


CString MyString;
char MyBuffer[100];
char Return=0x0D;
char LineFeed=0x0A;
CString Temp;
CString Holder[40000];

bool Flag;

DWORD RecordPointer,LengthOfFile,k,Starting,Ending,Looking;


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFFile01Dlg dialog

CFFile01Dlg::CFFile01Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CFFile01Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFFile01Dlg)
m_EDIT_MiddleInitial = _T("");
m_EDIT_FirstName = _T("");
m_EDIT_LastName = _T("");
m_EDIT_IDNumber = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFFile01Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFFile01Dlg)
DDX_Text(pDX, IDC_EDIT_MiddleInitial, m_EDIT_MiddleInitial);
DDX_Text(pDX, IDC_EDIT_FirstName, m_EDIT_FirstName);
DDX_Text(pDX, IDC_EDIT_LastName, m_EDIT_LastName);
DDX_Text(pDX, IDC_EDIT_IDNumber, m_EDIT_IDNumber);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFFile01Dlg, CDialog)
//{{AFX_MSG_MAP(CFFile01Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_Clear, OnBUTTONClear)
ON_BN_CLICKED(IDC_BUTTON_Quit, OnBUTTONQuit)
ON_BN_CLICKED(IDC_BUTTON_Write, OnBUTTONWrite)
ON_BN_CLICKED(IDC_BUTTON_BOF, OnButtonBof)
ON_BN_CLICKED(IDC_BUTTON_EOF, OnButtonEof)
ON_BN_CLICKED(IDC_BUTTON_Next, OnBUTTONNext)
ON_BN_CLICKED(IDC_BUTTON_Previous, OnBUTTONPrevious)
ON_BN_CLICKED(IDC_BUTTON_Search, OnBUTTONSearch)
ON_BN_CLICKED(IDC_BUTTON_Sort, OnBUTTONSort)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFFile01Dlg message handlers

BOOL CFFile01Dlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog.  The framework does this automatically
//  when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here


//CHANGE LOCATION OF DATABASE FILE HERE!!!!!!!!






// to change, list the folder (whatever drive its in, then a : (colon)
// then followed by two \\ (slashes), and another two \\ (slashes)
// if you want it to write into another folder in your drive.
// for example c:\\data\\myprograms\\database.dat
// And of course you end with a file name.dat



MyFile.Open("c:\\database.dat", CFile::modeCreate |
  CFile::modeReadWrite |
  CFile::modeNoTruncate);



















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

void CFFile01Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CFFile01Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CFFile01Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CFFile01Dlg::OnBUTTONClear()
{
// this will clear all of the fields

m_EDIT_FirstName="";
m_EDIT_LastName="";
m_EDIT_IDNumber="";
m_EDIT_MiddleInitial="";
UpdateData(false);

}

void CFFile01Dlg::OnBUTTONQuit()
{
// This will quit the program
MyFile.Close();
int Answer=MessageBox("Are You Sure" , "Quit Window",
  MB_YESNO+
  MB_ICONINFORMATION+
  MB_DEFBUTTON2);
if (Answer==IDYES) OnOK();

}

void CFFile01Dlg::OnBUTTONWrite()
{
//This writes to your database file located in your c drive
UpdateData(true);
MyFile.SeekToEnd();
m_EDIT_IDNumber=m_EDIT_IDNumber+"     ";
m_EDIT_IDNumber=m_EDIT_IDNumber.Left(5);
m_EDIT_FirstName=m_EDIT_FirstName+"                                 ";
m_EDIT_FirstName=m_EDIT_FirstName.Left(15);
m_EDIT_LastName=m_EDIT_LastName+"                              ";
m_EDIT_LastName=m_EDIT_LastName.Left(20);
m_EDIT_MiddleInitial=m_EDIT_MiddleInitial+"                         ";
m_EDIT_MiddleInitial=m_EDIT_MiddleInitial.Left(1);
MyFile.Write(m_EDIT_IDNumber+m_EDIT_FirstName+m_EDIT_LastName+m_EDIT_MiddleInitial+Return+LineFeed,43);
MessageBox("Record is Written");
m_EDIT_IDNumber="";
m_EDIT_FirstName="";
m_EDIT_LastName="";
m_EDIT_MiddleInitial="";
OnBUTTONSort();
UpdateData(false);
}

void CFFile01Dlg::OnButtonBof()
{
// This shows what is at the beggining of your database file
MyFile.SeekToBegin();
MyFile.Read(MyBuffer,43);
MyString=MyBuffer;
m_EDIT_IDNumber=MyString.Left(5);
m_EDIT_FirstName=MyString.Mid(5,15);
m_EDIT_LastName=MyString.Mid(20,20);
m_EDIT_MiddleInitial=MyString.Mid(40,1);
RecordPointer=1;
UpdateData(false);
}

void CFFile01Dlg::OnButtonEof()
{

//This shows what is at the end of your database file
MyFile.Seek(-43,CFile::end);
MyFile.Read(MyBuffer,43);
MyString=MyBuffer;
m_EDIT_IDNumber=MyString.Left(5);
m_EDIT_FirstName=MyString.Mid(5,15);
m_EDIT_LastName=MyString.Mid(20,20);
m_EDIT_MiddleInitial=MyString.Mid(40,1);
RecordPointer=MyFile.GetLength()/43;
UpdateData(false);
}

void CFFile01Dlg::OnBUTTONNext()
{
//This shows what is next in your database file

RecordPointer++;
LengthOfFile=MyFile.GetLength()/43;
if(RecordPointer > MyFile.GetLength()/43)
{
MessageBox("End of file");
OnButtonEof();
}
else
{

MyFile.Seek(0,CFile::current);
MyFile.Read(MyBuffer,43);
MyString=MyBuffer;
m_EDIT_IDNumber=MyString.Left(5);
m_EDIT_FirstName=MyString.Mid(5,15);
m_EDIT_LastName=MyString.Mid(20,20);
m_EDIT_MiddleInitial=MyString.Mid(40,1);
UpdateData(false);

}
}

void CFFile01Dlg::OnBUTTONPrevious()
{
//this shows what is previous in your database file
RecordPointer--;
if(RecordPointer < 1)
{
MessageBox("Beginning of file");
OnButtonBof();
}
else
{
MyFile.Seek(-86,CFile::current);
MyFile.Read(MyBuffer,43);
MyString=MyBuffer;
m_EDIT_IDNumber=MyString.Left(5);
m_EDIT_FirstName=MyString.Mid(5,15);
m_EDIT_LastName=MyString.Mid(20,20);
m_EDIT_MiddleInitial=MyString.Mid(40,1);
UpdateData(false);
}
}

void CFFile01Dlg::OnBUTTONSearch()
{
//this searches your database file for the ID Number that you entered
int ktr=0;
OnBUTTONSort();
UpdateData(true);
Starting=0;
Ending=MyFile.GetLength()/43;
Flag=true;
while (Flag)
{
ktr++;
Looking=Starting+(Ending-Starting)/2;
MyFile.Seek(Looking*43,CFile::begin);
MyFile.Read(MyBuffer,43);
MyString=MyBuffer;
if(atol(MyString) > atol(m_EDIT_IDNumber) )
{
Ending=Looking;
}
else if (atol(MyString.Left(5)) < atol(m_EDIT_IDNumber) )
{
Starting=Looking;
}
else if (atol(MyString.Left(5)) == atol(m_EDIT_IDNumber) )
{
MessageBox("Record is Found!!");
m_EDIT_IDNumber=MyString.Left(5);
m_EDIT_FirstName=MyString.Mid(5,15);
m_EDIT_LastName=MyString.Mid(20,20);
m_EDIT_MiddleInitial=MyString.Mid(40,1);
Flag=false;
UpdateData(false);
}
if (ktr>32)
{
Flag=false;
MessageBox("Record DOES NOT EXIST");
}
}

}

void CFFile01Dlg::OnBUTTONSort()
{
//This is not actually visible in the program, but is called by the
//search button to sort the database file in order by ID Number
MyFile.SeekToBegin();
for(k=0;k<MyFile.GetLength()/43;k++)
{
MyFile.Seek(0,CFile::current);
MyFile.Read(MyBuffer,43);
MyString=MyBuffer;
Holder[k]=MyString.Left(43);
}
LengthOfFile=MyFile.GetLength()/43;
Flag=true;
while(Flag)
{
Flag=false;
LengthOfFile--;
for (k=0;k<LengthOfFile;k++)
{
if(atol(Holder[k])>atol(Holder[k+1]))
{
Flag=true;
Temp=Holder[k];
Holder[k]=Holder[k+1];
Holder[k+1]=Temp;
}
}
}

MyFile.SeekToBegin();

for(k=0;k<MyFile.GetLength()/43;k++)
{
MyFile.Seek(0,CFile::current);
MyFile.Write(Holder[k],43);
}



  }

Project Homepage: