Question
Part2 Design and implement a C++ program (book7.cpp to book10.cpp) to read a file, to do the following tasks. You may continue this Part2 from
Part2
Design and implement a C++ program (book7.cpp to book10.cpp) to read a file, to do the following tasks.
You may continue this Part2 from Part1, or to create a new sqlite3 database with the sql statements to create book table and to insert the records (as shown in Part1).
You should design and implement C++ class object (book) corresponding to the database table book. The class should contain each member variable (corresponding to each table field name). For each member variable, you should define get & set method (for example, getAuthor and setAuthor for author in the table book, along with constructor method and destructor method.
Task#7. Design and implement C/C++ program (book7.cpp) to select all the records from table book and create each object of class book corresponding to each record from the table book.
Task#8. Design and implement C/C++ program (book8.cpp) to have a C++ static variable (bookMap) which is an associative map, to keep track of (the reference of) each book object (from Task#7) so that you may enumerate (iterate) each object. The map keeps track of the pair of (a) book title as key and (b) its reference to the corresponding object of the book. Then output the content of the map, sorted by book title and output each object.
Task#9. Design and implement C/C++ program (book9.cpp) to iterate each book object in the map, to write each object into a binary file (book.bin). Then delete all the book objects and empty all the entries in the map.
Task #10. Then your program (book10.cpp) is to read this binary file (book.bin), reading each book object from the file into a new book object, and create an entry to the map as you have done in Task #8, and then to output the content of the map to show that the program has done this task.
Task #11. Provide Makefile file to compile your program.
Part2.
Place your answer here for (a) your program listing and (b) its runs, with a proper headings for each part, and then also upload (submit) this document and all your codes and run-log in a .zip file.
Part2. Your program code - listing here.
|
Part2. Makefile listing
|
Part2. Your program runs for Tasks mentioned above. Place here only the relevant run log only, and for each task with a proper heading.
|
Sample codes that you may use for your base for this programming.
#include
#include
#include
#include
#include
using namespace std;
int main(int argc, char* argv[])
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
const char* data = "Callback function called";
/* Open database */
rc = sqlite3_open("test.db", &db);
if( rc ){
fprintf(stderr, "Can't open database: %s ", sqlite3_errmsg(db));
exit(0);
}else{
fprintf(stderr, "Opened database successfully ");
}
/* Create SQL statement */
sqlite3_stmt *statement;
char *query = "select * from COMPANY;";
if ( sqlite3_prepare(db, query, -1, &statement, 0 ) == SQLITE_OK )
{
int ctotal = sqlite3_column_count(statement);
int res = 0;
while ( 1 )
{
res = sqlite3_step(statement);
if ( res == SQLITE_ROW )
{
for ( int i = 0; i < ctotal; i++ )
{
string s = (char*)sqlite3_column_text(statement, i);
// print or format the output as you want
cout << s << " " ;
}
cout << endl;
}
if ( res == SQLITE_DONE || res==SQLITE_ERROR)
{
cout << "done " << endl;
break;
}
}
}
sqlite3_close(db);
return 0;
} Sample codes that you may use for your base for this programming.
// sample c++ code for object to binary file IO.
// try c++ to compile this program
#include
#include
using namespace std;
class Student
{
public:
char name[10];
char address[10];
char Gender;
char DOB[10];
Student()
{}
};
int main()
{
cout<<" Writting on file: Student1.txt ";
Student *p=new Student;
cout<<1<<": ";
cin>>p->name;
cout<<" ";
// to append use ios::app
// ofstream osfile("Student1.txt",ios::binary|ios::app);
ofstream osfile("Student1.txt",ios::binary);
osfile.write((char*)p,sizeof(Student));
osfile.close();
cout<<" reading Student1.txt ";
Student *p2=new Student;
ifstream isfile("Student1.txt",ios::binary);
isfile.read((char*)p2,sizeof(Student));
isfile.seekg(0);
isfile.close();
cout<<1<<": ";
cout<
cout<<" ";
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started