Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the following program in order you work with files mapped in memory.Pro

ID: 3820134 • Letter: C

Question

Complete the following program in order you work with files mapped in memory.Program will map a binary file (people.bin) that has struct Person records (used in program 4) for listing thefile and searching persons in the file.The mapping will be stored in a struct Memory variable the that keeps the pointer to memory and the size of thememory block.Functionality:


void print(const struct Person &p): It will print the person p.

(Implemented)
void mapFile(struct Memory &memo): It will map the file "people.bin" inmemory and it will return the mapping in the by reference parameter memo (ptrand size).

   void unmapFile(struct Memory &memo): It will unmap the file from memory.


void list(const struct Memory &memo) : It will print all the people in themapped file that it is in the memo parameter.

bool lookFor(string name, const struct Memory &memo, struct Person &p): Itwill search for a person named name in the mapped file that it is in the memo parameter. If found it will return true and the by reference parameter p will befilled with all the information of the person. If not found it will return false.


/* * File:   Program9.cpp * Author: *

* Created on April XX, 2017, hh:mm AM/PM */

#include <iostream>

#include <cstdlib>

#include <cstdio>

#include <string>

#include <cstring>

#include <unistd.h>

#include <sys/mman.h>

#include <sys/stat.h>

#include <fcntl.h>

struct Person {  

  char name[80];

    int age;
    char sex; /* M = Male and F = Female */

char lookingFor; /* M = Male and F = Female */

    char hobby[80];

};


struct Memory {

    void *ptr;

int size;

};

void print(const struct Person &p);

void mapFile(struct Memory &memo);

void unmapFile(struct Memory &memo);

void list(const struct Memory &memo);

bool lookFor(string name, const struct Memory &memo, struct Person &p);

int main(int argc, char** argv) {

    struct Memory memo;

struct Person p;

        mapFile(memo);

   list(memo);
        cout << "Looking for Jane ... " << endl;

    if (lookFor("Jane",memo, p)) print(p);

   else                         cout << "Jane NOT found" << endl;
        cout << endl << "Looking for James ... " << endl;

    if (lookFor("James",memo, p)) print(p);

   else                          cout << "James NOT found" << endl;
    

    unmapFile(memo);

    return 0;}

void print(const struct Person &p) {

        cout << "Name:        " << p.name << endl;

        cout << "Age:         " << p.age << endl;

        cout << "Sex:         " << p.sex << endl;

        cout << "Looking For: " << p.lookingFor << endl;

       cout << "Hobby:       " << p.hobby << endl;

}void mapFile(struct Memory &memo) {

}

void unmapFile(struct Memory &memo) {


}

void list(const struct Memory &memo) {

}

bool lookFor(string name, const struct Memory &memo, struct Person &p){

       return false;}

Execution Example:
Name:        John

Age:         20

Sex:         M

Looking For: F

Hobby:       Basketball

Name:        Jane

Age:         21

Sex:         F

Looking For: M

Hobby:       Basketball

Name:        Peter

Age:         22

Sex:         M

Looking For: F

Hobby:       Football

Name:        Mary

Age:         20

Sex:         F

Looking For: M

Hobby:       Art

Name:        Bob

Age:         21

Sex:         M

Looking For: F

Hobby:       Baseball

Name:        Bill

Age:         24

Sex:         M

Looking For: F

Hobby:       baseball

Looking for Jane ...

Name:        Jane

Age:         21

Sex:         F

Looking For: M

Hobby:       Basketball

Looking for James ...

James NOT found

Explanation / Answer

Hi,

Please find the code for the fucntions below:-

/* * File: Program9.cpp * Author: *
* Created on April XX, 2017, hh:mm AM/PM */
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
struct Person {
char name[80];
int age;
char sex; /* M = Male and F = Female */
char lookingFor; /* M = Male and F = Female */
char hobby[80];
};

struct Memory {
void *ptr;
int size;
};
void print(const struct Person &p);
void mapFile(struct Memory &memo);
void unmapFile(struct Memory &memo);
void list(const struct Memory &memo);
bool lookFor(string name, const struct Memory &memo, struct Person &p);
int main(int argc, char** argv) {
struct Memory memo;
struct Person p;
mapFile(memo);
list(memo);
cout << "Looking for Jane ... " << endl;
if (lookFor("Jane",memo, p)) print(p);
else cout << "Jane NOT found" << endl;
cout << endl << "Looking for James ... " << endl;
if (lookFor("James",memo, p)) print(p);
else cout << "James NOT found" << endl;
  
unmapFile(memo);
return 0;}
void print(const struct Person &p) {
cout << "Name: " << p.name << endl;
cout << "Age: " << p.age << endl;
cout << "Sex: " << p.sex << endl;
cout << "Looking For: " << p.lookingFor << endl;
cout << "Hobby: " << p.hobby << endl;
}void mapFile(struct Memory &memo)
{
/Create a file_mapping object
std::file_mapping mapping("/people.bin", std::memory_mappable::read_write);

//Create a mapped_region covering the whole file
std::mapped_region region(mapping, std::mapped_region::read_write);

//Obtain the size and the address of the mapped region
*memo.ptr = region.get_address();
std::size_t memo.size = region.get_size();

//Set the whole file
std::memset(memo.ptr, 0xFF, memo.size);
  
}
void unmapFile(struct Memory &memo)
{
free (memo)
}
void list(const struct Memory &memo)
{
for (int i=0;i<7;i++)
{
/*The list is accessed by the pointer to structure */
cout << "Name: " << memo->name << endl;
cout << "Age: " << memo->age << endl;
cout << "Sex: " << memo->sex << endl;
cout << "Looking For: " << memo->lookingFor << endl;
cout << "Hobby: " << memo->hobby << endl;
memo+1;
}
}
bool lookFor(string name, const struct Memory &memo, struct Person &p)
{
  
return false;
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote