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
|
#include <iostream>
#include <fstream>
#include <sstream>
#include "book.h"
using std::ifstream;
using std::ofstream;
void GetBooksFromFile(Book* bookList, int& booksFound)
{
ifstream inStream;
inStream.open("library_holdings.txt");
// open the file
if (inStream.fail() == false)
{
std::string theLine;
// read a line at a time
while (std::getline(inStream, theLine))
{
size_t oldPos = 0;
size_t newPos = theLine.find_first_of(oldPos);
bookList[booksFound].setCatalogue_Number(theLine.substr(oldPos, newPos - oldPos));
oldPos = newPos + 1;
newPos = theLine.find_first_of(oldPos);
bookList[booksFound].setAuthor_Last_Name(theLine.substr(oldPos, newPos - oldPos));
oldPos = newPos + 1;
newPos = theLine.find_first_of(oldPos);
bookList[booksFound].setAuthor_First_Name(theLine.substr(oldPos, newPos - oldPos));
oldPos = newPos + 1;
newPos = theLine.find_first_of(oldPos);
bookList[booksFound].setBook_Title(theLine.substr(oldPos, newPos - oldPos));
oldPos = newPos + 1;
newPos = theLine.find_first_of(oldPos);
bookList[booksFound].setGenre(theLine.substr(oldPos, newPos - oldPos));
oldPos = newPos + 1;
newPos = theLine.find_first_of(oldPos);
bookList[booksFound].setAvailability(theLine.substr(oldPos, newPos - oldPos));
theLine.clear();
booksFound++;
}
}
else
{
std::cout <<"Input file opening failed.\n";
exit(1);
}
inStream.close();
int main()
{
Book bookList[50]; // enough to hold 50 books
int numBooksFound = 0;
GetBooksFromFile (bookList, numBooksFound);
ofstream outStream;
outStream.open("computational_task_1.txt");
if(outStream.fail() == false)
{
for (int i=0; i<numBooksFound; i++)
{
outStream << bookList[i].getCatalogue_Number() << ","
<< bookList[i].getAuthor_Last_Name() << ","
<< bookList[i].getAuthor_First_Name() << ","
<< bookList[i].getBook_Title() << ","
<< bookList[i].getGenre() << ","
<< bookList[i].getAvailability() << "\n";
}
}
else
{
std::cout <<"Output file opening failed.\n";
exit(1);
}
|