Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CFile troubles

Status
Not open for further replies.

Bones3

Programmer
Jul 27, 2003
151
0
0
US
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 &quot;Client.h&quot;


using namespace std;

int main ()
{
CObList myClientList;
POSITION pos;
CClient *pClient;
int i;
char resp;
int nbrClients;

CFile Save;
CString SaveName = &quot;Sav.dat&quot;;
CClient bfrClient;
int clSize = sizeof(bfrClient);


for (;;)
{
cout << &quot;During this session, would you like to\n1. Start with a new file.\n2. Load existing file.\n : &quot;;
cin >> resp;
if (resp == '1')
{
if (Save.Open(SaveName, CFile::modeCreate | CFile::modeReadWrite))
{
cout << &quot;\nNew file opened\n&quot;;
break;
}
else
{
cout << &quot;\nError, problem opening new file!\n&quot;;
return 0;
}
}
if (resp == '2')
{
if (Save.Open(SaveName, CFile::modeReadWrite))
{
cout << &quot;\nExisting file opened.\n&quot;;
break;
}
else
{
cout << &quot;\nError, problem opening existing file!\n&quot;;
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 << &quot;\nOPTIONS: A'ddTail, I'nsertBefore,\nG'etAt, L'istAll, R'emoveAt, Q'uit\n : &quot;;
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 << &quot;\nBefore what position? : &quot;;
cin >> i;
if (i >= myClientList.GetCount())
{
cout << &quot;\nImpossible to insert here&quot;;
break;
}
pClient = new CClient;
pClient->Input();
pos = myClientList.FindIndex(i);
myClientList.InsertBefore(pos, pClient);
break;
case 'G':
cout << &quot;\n Get client at position (0, 1,...) : &quot;;
cin >> i;
nbrClients = myClientList.GetCount();
pos = myClientList.FindIndex(i);
pClient = (CClient*) myClientList.GetAt(pos);
cout << &quot; &quot;;
pClient->Display();
break;
case 'L':
pos = myClientList.GetHeadPosition();
nbrClients = myClientList.GetCount();
for (i = 0; i < nbrClients; i++)
{
pClient = (CClient*)myClientList.GetNext(pos);
cout << &quot;\n&quot; << i << &quot;: &quot;;
pClient->Display();
}
cout << &quot;\n&quot;;
break;
case 'R':
cout << &quot;\nRemove at what position? (0, 1,...) : &quot;;
cin >> i;
pos = myClientList.FindIndex(i);
pClient = (CClient*) myClientList.GetAt(pos);
cout << &quot;\nRemoving:\n&quot;; 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
 
Try changing the following two lines of code.

Save.Read(&pClient, clSize);
should be
Save.Read(pClient, clSize);


Save.Write(&pClient, clSize);
should be
Save.Write(pClient, clSize);

The first parameter in these two calls is a pointer. Your pClient variable is already a pointer, so I don't know what the address of operator does in this case.

Hope this helps.
 
Ahh, I guess that does make sence, and it fixed that particular prolem (although I uncovered some more--so don't use this code anyone! lol). Thanks a lot bcravens, hopefully after some thought I will be back on my feet.

-Bones
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top