Question: Please provide all steps also include snapshots: Warning: Y ou should not use system() function call throughout this assignment . Note. You may use some
Please provide all steps also include snapshots:
Warning: You should not use "system()" function call throughout this assignment.
Note. You may use some sample code provided (see below) for this part.
Design and implement a C++ program (a3part2.cpp) with the password file (/etc/passwd) to do the following tasks.
Task1.
Design and implement a class (passwd) which contains member variables for each field of the password file (/etc/passwd). You will have an array (passwdArray) of which each element is an instance-object of passwd class (from each record of the passwd file). Reading the password file (/etc/passwd), each record will be instantiated as an object of the passwd class and set to be an element of an array.
Output each record (object) in the array (passwdArray) to a file (a2p2task1.txt).
Object 1
user: root
uid: 0
gid: 0
gecos: root
home: /root
shell: /bin/bash
Object 2
Task2
Output each passwd object (in the passwdArray) into a binary file (a2p2task2.bin this binary file is an binary output of passwd objects).
Then your program is to read this binary output file (a2p2task2.bin), reading each passwd object from the file, into another password array (passwdArray2). Then output each object as you have done in Task1 to a file (a2p2task2.txt).
Get the output of a2p2task2.bin file using a few commands shown below:
ls l a2p2task2.bin
file a2p2task2.bin
od a2p2task2.bin | head
cat a2p2task2.bin | head
Task #3
Create a Map object (passwdMap) where its key is uid and its value is gid. Populate map entry from each object in passwdArray. Print each entry in passwdMap in ascending order by the key-value.
Task #4
Provide a Makefile file to compile your program.
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;
}
Map - Usage
The following code demonstrates how to use the map
#include
#include
#include
int main()
{
std::map
std::string s;
while (std::cin >> s && s != "end")
++wordcounts[s];
while (std::cin >> s && s != "end")
std::cout << s << ' ' << wordcounts[s] << ' ';
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
