
Patrick B. answered 05/13/20
Math and computer tutor/teacher
/************************************************************************************/
using namespace std;
#include <iostream>
#include <string>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MONSTER_NAME_LEN (255)
#define MONSTER_COLOR_LEN (255)
#define MAX_NUM_MONSTERS (10)
typedef struct _TMonsterRec
{
char monsterName[MONSTER_NAME_LEN];
int monsterWeight;
double monsterHeight;
char monsterColor[MONSTER_COLOR_LEN];
} * TMonsterRec;
#define MONSTER_REC_SIZE (sizeof(struct _TMonsterRec)+1)
void Monster_Input( TMonsterRec monsterRec)
{
cout << " Please input the monster's name :>";
cin >> monsterRec->monsterName;
cout << "Please input the Monster's Weight :>";
cin >> monsterRec->monsterWeight;
cout << "Please input the Monster's Height :>";
cin >> monsterRec->monsterHeight;
cout << "Please input the monster's color :>";
cin >> monsterRec->monsterColor;
};
void Monster_Output(TMonsterRec monsterRec, char * debugMsg)
{
if (debugMsg!=NULL)
{
cout << "*************************************************" << endl;
cout << debugMsg << endl;
}
cout << "**************************************************************"<<endl;
cout << " Monster Name :>" << monsterRec->monsterName << endl;
cout << " Monster Height " << monsterRec->monsterHeight << endl;
cout << " Monster Weight " << monsterRec->monsterWeight << endl;
cout << " Monster color:>" << monsterRec->monsterColor << endl;
cout << "***************************************************************" << endl;
};
inline void MonsterSerializeToCSV(TMonsterRec monsterRec, char *monsterRecBuff)
{
sprintf(monsterRecBuff,"%s,%d,%6.2lf,%s",monsterRec->monsterName,
monsterRec->monsterWeight,
monsterRec->monsterHeight,
monsterRec->monsterColor
);
}
void MonsterParseCSVBuff(TMonsterRec monsterRec, char * monsterRecBuff)
{
char tokenBuff[255];
strcpy(monsterRec->monsterName,strtok(monsterRecBuff,","));
strcpy(tokenBuff,strtok(NULL,","));
monsterRec->monsterWeight = atoi(tokenBuff);
strcpy(tokenBuff,strtok(NULL,","));
monsterRec->monsterHeight = atof(tokenBuff);
strcpy(monsterRec->monsterColor,strtok(NULL,","));
}
int Monster_Read(TMonsterRec monsterArray, char * filename)
{
FILE * fptr;
int iReturn=0;
fptr = fopen(filename,"r");
int numMonsterRecs;
char inbuff[MONSTER_REC_SIZE];
if (fptr!=NULL)
{
fscanf(fptr,"%d\n",&numMonsterRecs);//reads the # of monster records and eats the new line
if (numMonsterRecs>MAX_NUM_MONSTERS) { numMonsterRecs=MAX_NUM_MONSTERS; }
iReturn = numMonsterRecs;
for (int iLoop=0; iLoop<numMonsterRecs; iLoop++)
{
fgets(inbuff,MONSTER_REC_SIZE,fptr);
MonsterParseCSVBuff(&monsterArray[iLoop],inbuff);
}
fclose(fptr);
}
else
{
iReturn=-1;
}
return(iReturn);
};
int Monster_Write(TMonsterRec monsterArray, char * filename,int numMonsterRecs)
{
FILE * fptr;
int iReturn=0;
fptr = fopen(filename,"w");
char outbuff[MONSTER_REC_SIZE];
if (fptr!=NULL)
{
iReturn=numMonsterRecs; //writes the # of monster records
fprintf(fptr,"%d\n",numMonsterRecs);
for (int iLoop=0; iLoop<numMonsterRecs; iLoop++)
{
MonsterSerializeToCSV(&monsterArray[iLoop],outbuff);
fputs(outbuff,fptr);
}
fclose(fptr);
}
else
{
iReturn=-1;
}
return(iReturn);
}
int main()
{
int numMonsters;
struct _TMonsterRec monsters[MAX_NUM_MONSTERS];
numMonsters = Monster_Read(monsters,(char *)"monsters.dat");
char debugMsg[25];
for (int iLoop=0; iLoop<numMonsters; iLoop++)
{
sprintf(debugMsg,(char *)" Monster # %d ",(iLoop+1));
Monster_Output(&monsters[iLoop],debugMsg);
}
Monster_Write(monsters,(char*)"monsters.txt",numMonsters);
}
//************* SAMPLE INPUT FILE *******************************************/
4
Sully,160,5.83,Green:Orange:Purple
BobaFet,854,8.33,Gray & slimy
Yoda,32,2.16,Green:Gray:old
MadMax,2345,100,black