Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part3 C++ passwd & binary IO Warning: You should not use system() function call throughout this assignment. Note. You may use some sample code provided

Part3 C++ passwd & binary IO

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 (a3part3.cpp) with the password file (/etc/passwd) to do the following tasks.

Note. To compile a C++ program, use g++. If you use gcc, use: -lstdc++ option

gcc hello.cpp -o hello -lstdc++

Source: https://gcc.gnu.org/projects/cxx-status.html

Task #1.

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 (a2p3task1.txt).

Object 1

user: root

uid: 0

gid: 0

gecos: root

home: /root

shell: /bin/bash

Object 2

Task #2

Output each passwd object (in the passwdArray) into a binary output file (a2p2task3.bin this binary file is an binary output of passwd objects).

Then your program is to read this binary output file (a2p3task2.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 (a2p3task2.txt).

Get the output of a2p3task2.bin file using a few commands shown below:

ls l a2p3task2.bin

file a2p3task2.bin

od a2p3task2.bin | head

cat a2p3task2.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.

Part3 Solution

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.

Part3. Your program code - listing here.

Part3. Task1. Your program runs for Tasks mentioned above.

Place here only the relevant run log only, and for each task with a proper heading.

Part3. Task2. Your program runs for Tasks mentioned above.

Place here only the relevant run log only, and for each task with a proper heading.

Part3. Task2. Place the output of a2p3task2.bin file using a few commands shown below:

ls l a2p2task2.bin

od a2p2task2.bin | head

file a2p2task2.bin

Part3. Task3. Your program runs for Tasks mentioned above.

Place here only the relevant run log only, and for each task with a proper heading.

Part3. Task4. Makefile listing

Submit this document and a zip file containing all the files (including your program source file, binary executable, output files, and Makefile).

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<name;

cout<<" ";

return 0;

}

** http://en.wikipedia.org/wiki/Associative_containers

Map - Usage

The following code demonstrates how to use the map to count occurrences of words. It uses the word as the key and the count as the value.

 
#include 
#include 
#include 
 
int main()
{
 std::map wordcounts;
 std::string s;
 
 while (std::cin >> s && s != "end")
 ++wordcounts[s];
 
 while (std::cin >> s && s != "end")
 std::cout << s << ' ' << wordcounts[s] << ' ';
}

When executed, the user first types a series of words separated by spaces, and a word "end" to signify the end of input; then the user can input words to query how many times each word occurred in the previous series.

The above example also demonstrates that the operator [ ] inserts new objects (using the default constructor) in the map if there isn't one associated with the key. So integral types are zero-initialized, strings are initialized to empty strings, etc.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxviii Special Issue On Database And Expert Systems Applications Lncs 9940

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Qimin Chen

1st Edition

3662534541, 978-3662534540

More Books

Students also viewed these Databases questions