Design and implement a C/C++ program (a3part2.c [or a3part2.cpp]) to read a file
ID: 3858372 • Letter: D
Question
Design and implement a C/C++ program (a3part2.c [or a3part2.cpp]) to read a file, to do the following tasks. Task1. Read the file (/etc/passwd) and output for each line to the file named (a3p2task1.txt) For example, for one entry in the /etc/passwd file contains the following line:
For example, for one entry in the /etc/passwd file contains the following line:
richard:*:1000:513:CS Professor:/home/richard:/bin/bash
Then the program should output it as shown below.
userid: richard
uid: 1000
gid: 513
gecos: CS Professor
home directory: /home/richard
shell:/bin/bash
Task #2.
Design and implement each record as an object (of class named user).
Design and implement a C++ static variable (userMap) which is an associative map, to keep track of (the reference of each) user-account information so that you may enumerate (iterate) each object later. The map keeps track of the pair of (a) user-name (e.g., richard) and (b) its reference to the corresponding object (e.g., of richard).
Then output the content of the map – first (1) sorted by uid and output each object (as shown in Task1 output-format), and then (2) sorted by gid and output each object (as shown in Task1 output-format), to a file named (a3p2task2.txt).
Task #3.
Iterate each user object in the map, to write each object into a binary file (a3p2task3.bin).
Then delete all the user objects and empty all the entries in the map.
Task #4
Then your program is to read this binary file (a3p2task3.bin), reading each user object from the file into a new user object, and create an entry to the map as you have done in Task #2. Then output each object as you have done in Task#3 to a file (a3p2task4.txt).
You should provide a cross-checking of these two files with cmp command.
cmp a3p2task1.txt a3p2task4.txt
Task #5. 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 <iostream>
#include <fstream>
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<<p2->name;
cout<<" ";
return 0;
}
** http://en.wikipedia.org/wiki/Associative_containers
Map - Usage
The following code demonstrates how to use the map<string, int> to count occurrences of words. It uses the word as the key and the count as the value.
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.
For further information on map: http://www.cplusplus.com/reference/map/map/map/
Explanation / Answer
#include <iostream>
#include <fstream>
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<<p2->name;
cout<<" ";
return 0;
}
** http://en.wikipedia.org/wiki/Associative_containers
Map - Usage
The following code demonstrates how to use the map<string, int> to count occurrences of words. It uses the word as the key and the count as the value.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.