
Patrick B. answered 08/10/21
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
typedef
class Item
{
protected:
char * str;
public:
Item() { str=NULL; }
Item ( char* s)
{
int nSize = strlen(s)+1;
str = (char*)malloc(nSize);
strcpy(str,s);
}
~Item()
{
if (str!=NULL)
{
free(str);
}
}
Item(const Item& item)
{
int nSize = strlen(item.str)+1;
str = (char*) malloc(nSize);
strcpy(str,item.str);
}
void operator = (const Item & item)
{
int nSize = strlen(item.str)+1;
str = (char*) malloc(nSize);
strcpy(str,item.str);
}
int GetSize() { return(strlen(str)); }
void Get(char*itemBuff) { strcpy(itemBuff,str); }
void Destroy()
{
if (str!=NULL)
{
free(str);
}
}
} * TItem;
#define ITEM_SIZE (sizeof(Item))
int Collection_Read(vector<Item> *items, char * filename)
{
int iReturn=0;
FILE * fptr = fopen(filename,"r");
if (fptr!=NULL)
{
char inbuff[1024];
int N;
//reads the # of items in the file and eats the newline
fscanf(fptr,"%d",&N);
fgets(inbuff,1024,fptr);
//cout << N << " items in the file " << endl;
for (int iLoop=0; iLoop<N; iLoop++) //for each record in the fil....
{
//reads the next record, then sanitizes: removes trailing white space, newlines,etc
fgets(inbuff,1024,fptr);
char * tailPtr = inbuff+strlen(inbuff)-1;
while ((*tailPtr==' ') || (!isalnum(*tailPtr)))
{
*tailPtr=0;
tailPtr--;
}
Item newItem(inbuff); //cout << ">" << inbuff <<"<" << endl;
items->push_back(newItem);
memset(inbuff,0,1024);
}
fclose(fptr);
}
return(iReturn);
}
Collection_Output(vector<Item> * items, char * strTitleMsg=NULL)
{
int N = items->size();
if (strTitleMsg!=NULL)
{
cout << "*********************************************************" << endl;
cout << strTitleMsg<<endl;
}
cout << "***********************************************************"<<endl;
for (int iLoop=0; iLoop<N; iLoop++)
{
Item curItem = (Item)((*items)[iLoop]);
int nStrLen = curItem.GetSize();
char * itemBuff = (char*)malloc(nStrLen);
curItem.Get(itemBuff);
cout << itemBuff << endl;
free(itemBuff);
}
}
int Menu()
{
int x;
cout << "**********************************************" << endl;
cout << "* <[(1)]> INSERT /// ADD " << endl;
cout << "** <[(2)]> REMOVE///DELETE " << endl;
cout <<"**************************************************" <<endl;
cout << " Input selection or ZER0 to Quit :>";
cin >> x;
return x;
}
Collection_Write(vector<Item>* items, char * filename)
{
FILE * fptr = fopen(filename,"w");
int N = items->size();
fprintf(fptr,"%d \n",N);
for (int iLoop=0; iLoop<N; iLoop++)
{
char * outbuff;
Item curItem = (Item)((*items)[iLoop]);
int nStrLen = curItem.GetSize();
outbuff= (char*)malloc(nStrLen);
curItem.Get(outbuff);
fprintf(fptr,"%s \n",outbuff);
free(outbuff);
}
fclose(fptr);
}
int main()
{
vector<Item> items;
Collection_Read(&items,(char*)"E:\\itemlist.dat");
char strTitle[25];
int iMenuChoice=-1;
while (iMenuChoice!=0)
{
sprintf(strTitle,"%d items in the list",items.size());
Collection_Output(&items,strTitle);
iMenuChoice = Menu();
switch (iMenuChoice)
{
case 1:
{
char itemBuff[8192];
cout << " Input :> "; cin.ignore();
cin.getline(itemBuff,8192,'\n');
Item newItem(itemBuff);
items.push_back(newItem);
Collection_Write(&items, (char*)"E:\\item_list.dat");
break;
}
case 2:
{
int iIndexPos=-1;
while (iIndexPos<0 || iIndexPos>items.size())
{
cout << " record # ??? :>";
cin >> iIndexPos;
}
std::vector<Item>::iterator iIterator = items.begin();
items.erase(iIterator + iIndexPos);
Collection_Write(&items,(char*)"E:\\item_list.dat");
break;
}
}
}
}
/***************************************************************************/
home ~$ :> cat itemlist.dat
6
pineapple,watermelon,strawberries,grapes
52769
35.42
1234567890 streetname; cityname, ST; XXXXX-XXXXX
5 + (4-3*2)/7 + sqrt(1+pow(8,3)-sin(9*pi))+X;