I am having trouble getting the file either to dump data into the CObList or recieve data properly from the list, I cannot tell which.
#include <iostream>
#include <afxwin.h>
#include "Client.h"
using namespace std;
int main ()
{
CObList myClientList;
POSITION pos;
CClient *pClient;
int i;
char resp;
int nbrClients;
CFile Save;
CString SaveName = "Sav.dat";
CClient bfrClient;
int clSize = sizeof(bfrClient);
for (;
{
cout << "During this session, would you like to\n1. Start with a new file.\n2. Load existing file.\n : ";
cin >> resp;
if (resp == '1')
{
if (Save.Open(SaveName, CFile::modeCreate | CFile::modeReadWrite))
{
cout << "\nNew file opened\n";
break;
}
else
{
cout << "\nError, problem opening new file!\n";
return 0;
}
}
if (resp == '2')
{
if (Save.Open(SaveName, CFile::modeReadWrite))
{
cout << "\nExisting file opened.\n";
break;
}
else
{
cout << "\nError, problem opening existing file!\n";
return 0;
}
}
}
// dump file contents into list
int j = Save.GetLength() / clSize;
for (i = 0;i < j; i++)
{
Save.Seek(i * clSize, CFile::begin);
Save.Read(&pClient, clSize);
myClientList.AddTail(pClient);
}
do
{
cout << "\nOPTIONS: A'ddTail, I'nsertBefore,\nG'etAt, L'istAll, R'emoveAt, Q'uit\n : ";
cin >> resp;
resp = toupper(resp);
if (resp == 'Q')
{
break;
}
switch (resp)
{
case 'A':
pClient = new CClient;
pClient->Input();
myClientList.AddTail(pClient);
break;
case 'I':
cout << "\nBefore what position? : ";
cin >> i;
if (i >= myClientList.GetCount())
{
cout << "\nImpossible to insert here";
break;
}
pClient = new CClient;
pClient->Input();
pos = myClientList.FindIndex(i);
myClientList.InsertBefore(pos, pClient);
break;
case 'G':
cout << "\n Get client at position (0, 1,...) : ";
cin >> i;
nbrClients = myClientList.GetCount();
pos = myClientList.FindIndex(i);
pClient = (CClient*) myClientList.GetAt(pos);
cout << " ";
pClient->Display();
break;
case 'L':
pos = myClientList.GetHeadPosition();
nbrClients = myClientList.GetCount();
for (i = 0; i < nbrClients; i++)
{
pClient = (CClient*)myClientList.GetNext(pos);
cout << "\n" << i << ": ";
pClient->Display();
}
cout << "\n";
break;
case 'R':
cout << "\nRemove at what position? (0, 1,...) : ";
cin >> i;
pos = myClientList.FindIndex(i);
pClient = (CClient*) myClientList.GetAt(pos);
cout << "\nRemoving:\n"; pClient->Display();
myClientList.RemoveAt(pos);
delete pClient;
break;
default:
break;
}
}
while(true);
// dump information into file
pos = myClientList.GetHeadPosition();
nbrClients = myClientList.GetCount();
Save.modeCreate;
for (i = 0; i < nbrClients; i++)
{
pClient = (CClient*)myClientList.GetNext(pos);
Save.SeekToEnd();
Save.Write(&pClient, clSize);
}
return 0;
}
My goal was to store the client data in the file and be able to retrive it when the program is used again. The problem with my code is that when I go to view the list (option L) the program crashes. (or in debug mode displays a smily face with some numbers) I think it might be the fact that I am using a pointer as the buffer (&pClient), could that be it? If you need the CClient class to debug I can give it also.
-Bones
#include <iostream>
#include <afxwin.h>
#include "Client.h"
using namespace std;
int main ()
{
CObList myClientList;
POSITION pos;
CClient *pClient;
int i;
char resp;
int nbrClients;
CFile Save;
CString SaveName = "Sav.dat";
CClient bfrClient;
int clSize = sizeof(bfrClient);
for (;
{
cout << "During this session, would you like to\n1. Start with a new file.\n2. Load existing file.\n : ";
cin >> resp;
if (resp == '1')
{
if (Save.Open(SaveName, CFile::modeCreate | CFile::modeReadWrite))
{
cout << "\nNew file opened\n";
break;
}
else
{
cout << "\nError, problem opening new file!\n";
return 0;
}
}
if (resp == '2')
{
if (Save.Open(SaveName, CFile::modeReadWrite))
{
cout << "\nExisting file opened.\n";
break;
}
else
{
cout << "\nError, problem opening existing file!\n";
return 0;
}
}
}
// dump file contents into list
int j = Save.GetLength() / clSize;
for (i = 0;i < j; i++)
{
Save.Seek(i * clSize, CFile::begin);
Save.Read(&pClient, clSize);
myClientList.AddTail(pClient);
}
do
{
cout << "\nOPTIONS: A'ddTail, I'nsertBefore,\nG'etAt, L'istAll, R'emoveAt, Q'uit\n : ";
cin >> resp;
resp = toupper(resp);
if (resp == 'Q')
{
break;
}
switch (resp)
{
case 'A':
pClient = new CClient;
pClient->Input();
myClientList.AddTail(pClient);
break;
case 'I':
cout << "\nBefore what position? : ";
cin >> i;
if (i >= myClientList.GetCount())
{
cout << "\nImpossible to insert here";
break;
}
pClient = new CClient;
pClient->Input();
pos = myClientList.FindIndex(i);
myClientList.InsertBefore(pos, pClient);
break;
case 'G':
cout << "\n Get client at position (0, 1,...) : ";
cin >> i;
nbrClients = myClientList.GetCount();
pos = myClientList.FindIndex(i);
pClient = (CClient*) myClientList.GetAt(pos);
cout << " ";
pClient->Display();
break;
case 'L':
pos = myClientList.GetHeadPosition();
nbrClients = myClientList.GetCount();
for (i = 0; i < nbrClients; i++)
{
pClient = (CClient*)myClientList.GetNext(pos);
cout << "\n" << i << ": ";
pClient->Display();
}
cout << "\n";
break;
case 'R':
cout << "\nRemove at what position? (0, 1,...) : ";
cin >> i;
pos = myClientList.FindIndex(i);
pClient = (CClient*) myClientList.GetAt(pos);
cout << "\nRemoving:\n"; pClient->Display();
myClientList.RemoveAt(pos);
delete pClient;
break;
default:
break;
}
}
while(true);
// dump information into file
pos = myClientList.GetHeadPosition();
nbrClients = myClientList.GetCount();
Save.modeCreate;
for (i = 0; i < nbrClients; i++)
{
pClient = (CClient*)myClientList.GetNext(pos);
Save.SeekToEnd();
Save.Write(&pClient, clSize);
}
return 0;
}
My goal was to store the client data in the file and be able to retrive it when the program is used again. The problem with my code is that when I go to view the list (option L) the program crashes. (or in debug mode displays a smily face with some numbers) I think it might be the fact that I am using a pointer as the buffer (&pClient), could that be it? If you need the CClient class to debug I can give it also.
-Bones