Answered You can hire a professional tutor to get the answer.
IN UNIX: THIS IS MY CODE. I AM GETTING A SEGMENTATION ERROR INSTEAD OF READING THE FILE AND THAT IS THE ONLY PROBLEM I HAVE, SHOULD BE AN EASY...
IN UNIX: THIS IS MY CODE. I AM GETTING A SEGMENTATION ERROR INSTEAD OF READING THE FILE AND THAT IS THE ONLY PROBLEM I HAVE, SHOULD BE AN EASY PROBLEM TO FIX IF YOU KNOW CPP.
#include<iostream>
#include<string>
#include<fstream>
#include<cstring>
#include<cstdlib>
#include<map>
class Passwd{
private:
char user[50];
int uid;
int gid;
char gecos[50];
char home[50];
char shell[50];
public:
void setUid(int a){
uid = a;
}
void setGid(int a){
gid = a;
}
void setUser(char *a){
strcpy(user,a);
}
void setGecos(char *a){
strcpy(gecos,a);
}
void setHome(char *a){
strcpy(home,a);
}
void setShell(char *a){
strcpy(shell,a);
}
int getUid(){
return uid;
}
int getGid(){
return gid;
}
char *getGecos(){
return gecos;
}
char *getHome(){
return home;
}
char *getShell(){
return shell;
}
char *getUser(){
return user;
}
};
using namespace std;
int main(){
ifstream fin("passwd"); //thats the name of the file program is supposed to read. note that this program is executed in unix.
ifstream fin1;
ofstream fout;
char user[50];
int uid;
int gid;
char gecos[50];
char home[50];
char shell[50];
map <int, int> passMap;
map <int, int> :: iterator itr;
if (!fin){
cout << "Error oprning filen";
return 0;
}
string str;
Passwd data[100];
Passwd data1[100];
int count = 0;
while (getline(fin,str)){
char *ch;
ch = strtok((char *)str.c_str(), ":");
strcpy(user,ch);
ch = strtok(NULL, ":");
ch = strtok(NULL, ":");
uid = atoi(ch);
ch = strtok(NULL, ":");
gid = atoi(ch);
ch = strtok(NULL, ":");
strcpy(gecos,ch);
ch = strtok(NULL, ":");
strcpy(home,ch);
ch = strtok(NULL, ":");
strcpy(shell,ch);
data[count].setUser(user);
data[count].setUid(uid);
data[count].setGid(gid);
data[count].setGecos(gecos);
data[count].setHome(home);
data[count].setShell(shell);
count++;
}
fin.close();
for (int i = 0; i < count; i++){
cout << "user:" << data[i].getUser() << endl;
cout << "uid:" << data[i].getUid() << endl;
cout << "gid:" << data[i].getGid() << endl;
cout << "gecos:" << data[i].getGecos() << endl;
cout << "home:" << data[i].getHome() << endl;
cout << "shell:" << data[i].getShell() << endl;
cout << "-------------------------------------------n";
}
fout.open("passwd.bin",ios::out|ios::binary);
for (int i = 0; i < count; i++){
fout.write((char *)&data[i],sizeof(data[i]));
}
fout.close();
fin1.open("passwd.bin",ios::in|ios::binary);
for (int i = 0; i < count; i++){
fin1.read((char *)&data1[i],sizeof(data1[i]));
}
fin1.close();
for (int i = 0; i < count; i++){
cout << "user:" << data1[i].getUser() << endl;
cout << "uid:" << data1[i].getUid() << endl;
cout << "gid:" << data1[i].getGid() << endl;
cout << "gecos:" << data1[i].getGecos() << endl;
cout << "home:" << data1[i].getHome() << endl;
cout << "shell:" << data1[i].getShell() << endl;
cout << "-------------------------------------------n";
}
for (int i = 0; i < count; i++){
passMap.insert(pair <int, int> (data1[i].getUid(), data1[i].getGid()));
}
for (itr = passMap.begin(); itr != passMap.end(); ++itr){
cout << itr->first << " " << itr->second << endl;
}
cout << endl;
return 0;
}