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 90 91 92 93 94 95
|
// DVDMain.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "DVD.h"
int countDVD (fstream &);
int main()
{
string line;
int numDVD,i;
fstream dvdFile;
DVD *ptrDVDarray;
dvdFile.open("c:\\users\\public\\dvd.txt", ios::in|ios::binary);
numDVD = countDVD(dvdFile);
cout<<numDVD<<endl;
ptrDVDarray = new DVD[numDVD];
dvdFile.clear();
dvdFile.seekg(0L, ios::beg);
for (i=0; i < numDVD; i++)
{
getline(dvdFile, line, '\n');
(ptrDVDarray + i)->setTitle(line);
getline(dvdFile, line, '\n');
(ptrDVDarray + i)->setTime(line);
getline(dvdFile, line, '\n');
(ptrDVDarray + i)->setYear(line);
getline(dvdFile, line, '\n');
(ptrDVDarray + i)->setActors(line);
}
for (i=0; i < numDVD; i++)
{
cout<< (ptrDVDarray + i)->getTitle() <<endl;
cout<< (ptrDVDarray + i)->getTime() <<endl;
cout<< (ptrDVDarray + i)->getYear() <<endl;;
cout<< (ptrDVDarray + i)->getActors() <<endl;
}
delete ptrDVDarray;
return 0;
}
//*****Functions*********
int countDVD(fstream &infile)
{
int numDVD = 0; //hold the number of dvds
int newline = 0; //hold the number of '\n'
char nL;
//set read position at begining of file
infile.seekg(0L, ios::beg);
//set pos to value of current position
long pos = infile.tellg();
//Loop to read each character and
// search for '\n' till eof
while(!infile.eof())
{
//set read point to current position
infile.seekg(pos, ios::beg);
// get/copy character at curent position
nL = infile.get();
//test for '\n' and increment the '\n' counter (newline)
if (nL == '\n')
{
newline++;
//increment numDVD everytime newline hit 5
if(newline == 5)
{
numDVD++;
newline = 0;
}
}
// move to the next read position
pos++;
}//end while
return numDVD;
}
|