Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 888391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:23:23+00:00 2026-05-15T13:23:23+00:00

I need a solution for sorting of unix pwd file using C++ based on

  • 0

I need a solution for sorting of unix pwd file using C++ based on the last name. The format of the file is username, password, uid, gid, name, homedir, shell. All are seperated by colon delimiters. The name field contains first name follwed by last name both seperated by space I am able to sort the values using map and i am posting my code. Can some one suggest me improvements that I can do to my code please. Also I am unable to see the sorted lines in my file.

string line,item;
fstream myfile("pwd.txt");
vector<string> lines;
map<string,int> lastNames;
map<string,int>::iterator it;

if(myfile.is_open())
 {    
  char delim =':';
  int count =0;
  while(!myfile.eof())
  {
    count++;
    vector<string> tokens;
    getline(myfile,line);
    istringstream iss(line);

    lines.push_back(line);

    while(getline(iss,item,delim))
    {
        tokens.push_back(item);
    }
    cout<<tokens.size()<<endl;;
    size_t i =tokens[4].find(" ");
    string temp = tokens[4].substr(i,(tokens[4].size()-i));
    cout<<temp<<endl;

    lastNames.insert(pair<string,int>(temp,count));
    tokens.clear();

  }

    myfile.seekg(0,ios::beg);

    for(it=lastNames.begin();it!=lastNames.end();it++)
  {
         cout << (*it).first << " => " << (*it).second << endl;
         int value=lastNames[(*it).first ];
         myfile<<lines[value-1]<<endl;
         cout<<lines[value-1]<<endl;
         cout<<value<<endl;
  }

}

Also I am having problem writing to the file I am unable to see the sorted results.

my problem:

    Can someone please explain me why I am unable to see the written results in the file!

Thanks & Regards,

Mousey.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:23:23+00:00Added an answer on May 15, 2026 at 1:23 pm

    Since the format of the file is fixed

    username, password, uid, gid, first name(space)lastname, homedir, shell
    

    Maintain a std::map with key value as string (which will contain last name, and value as line number

    Start reading the file line by line, extract the last name (Split the line by “,” and then split fifth extracted part on space).

    Store the name along with line number in map

    When complete file has been read, just output the line numbers as mentioned in map. (Map contains lat names in sorted order)

    For splitting a string
    Refer to

    Split a string in C++?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.