1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include <iostream>
#include <fstream>
#include "exportImportStructDec.h"
using namespace std;
void PostOpenWrite(ofstream&,char [],Grades,Grades *);
void PostOpenWrite(ofstream& fileOpenWright,char openWriteMessage[],Grades structOpenWrite,Grades * pStructOpenWrite)
{
cout << openWriteMessage;
ScanStruct(pStructOpenWrite);
if (fileOpenWright.is_open())
{
fileOpenWright.write (reinterpret_cast<char*>(pStructOpenWrite), sizeof(structOpenWrite));
fileOpenWright.close();
}
else cout << "\nUnable to open file for output\n";
}
Grades * Allocate(char fileName[],Grades structAllocate,Grades * pStructAllocate)
{
//check the size of the file and allocate appropriate memory:
int firstBit,lastBit;
ifstream fileAllocate (fileName);
if (fileAllocate.is_open())
{
firstBit = fileAllocate.tellg();
fileAllocate.seekg (0, ios::end);
lastBit = fileAllocate.tellg();
fileAllocate.close();
int structsInFile=(lastBit-firstBit)/(sizeof(structAllocate));
pStructAllocate = new Grades [structsInFile];
return pStructAllocate;
}
else cout << "\nUnable to open file for size check\n";
}
//function to read all of the structures in a file, put them into memory, and display them on screen
void ReadFile(char readFileName[],Grades structi,Grades * pStructi)
{
pStructi=Allocate(readFileName,structi,pStructi);
//read the file into memory:
int structArraySize=0;
ifstream filei;
filei.open (readFileName, ios::in|ios::binary);
if (filei.is_open())
{
while ((filei.peek()!=EOF))
{
filei.read(reinterpret_cast<char*>(&pStructi[structArraySize]), sizeof(Grades));
structArraySize++;
}
//display memory on screen:
PrintMemory(structArraySize,pStructi);
}
else cout << "\nUnable to open file for input\n";
}
//function to write over a file with a new structure
void WrtFile(char wrtFileName[],char wrtMessage[],Grades structo,Grades * pStructo)
{
ofstream fileo;
fileo.open (wrtFileName, ios::out|ios::binary);
PostOpenWrite(fileo,wrtMessage,structo,pStructo);
}
//function to append the end of a file with a new structure
void AppFile(char appFileName[],char appMessage[],Grades structa,Grades * pStructa)
{
ofstream filea;
filea.open (appFileName, ios::app|ios::binary);
PostOpenWrite(filea,appMessage,structa,pStructa);
}
void ScanStruct(Grades * pStructScan)
{
cin >> (*pStructScan).grade1 >> (*pStructScan).grade2 >> (*pStructScan).grade3 >> (*pStructScan).grade4;
}
void PrintMemory(int specificStructArraySize,Grades * pStructi)
{
for(int i=0;i<specificStructArraySize;i++)
{
cout<<"\ngrade1\n"<< (pStructi[i]).grade1<<"\ngrade2\n"<< (pStructi[i]).grade2<<"\ngrade3\n"<< (pStructi[i]).grade3<<"\ngrade4\n"<< (pStructi[i]).grade4<<"\n";
}
}
| |